Some easy Questions about C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm working the first days with Visual C#.net. So most of you think these questions are really easy.

1. How can I read out the windows-account?
2. How can I read datas from an INI-file?

A short example will help me quiet good.

Thanks.
 
1. Not sure what you exactly mean but if you are looking for the current
user, you can use System.Environment.UserName
2. Read Data from a file using StreamReader

string myFilePath = @"C:\myfile.ini";
string myFileText = string.Empty;

using (System.IO.StreamReader sr = new System.IO.StreamReader(myFilePath ))
{
myFileText = sr.ReadToEnd();
sr.Close();
}

Your specifics may be different, you may be reading line by line, which is
equally simple. B the way if you want to store settings for your application
you'd probably be better off creating a configuration file...by using
Add->Configuration File to your project, which will create an app.config
file, which is nothing but an xml file that can be read easily, using
System.Configuration..............
 
You could use a WIN32 API call in your class.

Add:
using System.Runtime.InteropServices;

And at the top of the class define the WIN32 function and the DLL in which
it is carried:
[DllImport("kernel32")]

private static extern int GetPrivateProfileString(string section,

string key,string def, StringBuilder retVal,

int size,string filePath);

Now you can read the INI file like you did in C or C++.

HTH

Harry
 
Thanks for your help. But there is still a problem. I didn't programm reading an INI-file in c/c++ or in any programming language else. So I don't know how to do that.

Harry Whitehouse said:
You could use a WIN32 API call in your class.

Add:
using System.Runtime.InteropServices;

And at the top of the class define the WIN32 function and the DLL in which
it is carried:
[DllImport("kernel32")]

private static extern int GetPrivateProfileString(string section,

string key,string def, StringBuilder retVal,

int size,string filePath);

Now you can read the INI file like you did in C or C++.

HTH

Harry



Eifel-benz said:
I'm working the first days with Visual C#.net. So most of you think these questions are really easy.

1. How can I read out the windows-account?
2. How can I read datas from an INI-file?

A short example will help me quiet good.

Thanks.
 
Back
Top