Tronster's C# Frequently Asked Questions

How do I obtain the version # of my program running?
How do I find the version # of my assembly?
How do I find the version # that is shown in my program's property pages?
The following code obtains the version # of the assembly that is running. System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
Returned Value:
1.0.0.0
C# 2.0
How do I start an EXE from my C# program?
What's the C# equivalent to C++'s ShellExecute?
Use the static off of System.Diagnostics.Process class: System.Diagnostics.Process.Start(pathToFile)
C# 2.0
How do I obtain the folder all profiles are kept in?
How do I obtain the directory all profiles are kept in?
How do I obtain the directory of all users?
Short answer is you need to platform invoke (pinvoke) SHGetSpecialFolderPath with the current profile value and then chop off the directory on the end.
[DllImport("shell32.dll")]
    static extern bool SHGetSpecialFolderPath(
	IntPtr hwndOwner,[Out] StringBuilder lpszPath, int nFolder, bool fCreate);

const int CSIDL_PROFILE = 0x0028;
	
public void foo() {
    StringBuilder path = new StringBuilder(260);
    SHGetSpecialFolderPath(IntPtr.Zero, path, CSIDL_PROFILE, false);
    // Add code here to chop off the dir at the end of the path.
}
There is no direct API call that will return this folder. Calls made to Environment.GetFolderPath(Environment.SpecialFolder myFolder) will not obtain this folder because the enumeration for "SpecialFolder" doesn't include a profile-related value. Additionally there is no native Win32 API call that will return the value. While the method "SHGetSpecialFolderPath()" exists, and the enumeration that is passed to this method contains a value called "CSIDL_PROFILES", making the following call yields an empty path: SHGetSpecialFolderPath(IntPtr.Zero, path, (int)CSIDL_PROFILES, false); Fortunately there is a value in the enumation "CSIDL_PROFILE" (notice it's not plural like above) which does return the path for the currently running profile. So making this call and then removing the last directory in the path will give the directory / folder where profiles are kept.
Typical WinXP return value:
C:\Documents and Settings\billy
Typical Vista return value:
C:\Users\billy
C# 2.0, C# 1.1, C#1.0

Other FAQs




<<< Back