Get name of system folder [using C#]
-
Hi, I've a university issued laptop with windows 7 on it. When I click on "windows" button it show my name as one of the system folder, but there is no path for it. It is above the "My Document" folder, in fact everyone in the university has same folder with their name [obviously]. I want to make an program that could extract the name of this folder when its it run on a particular system. And computer name, account name or name of Environment.getFolderName(SpecialFolder.Personal) all are different from this folder. I do not know where is this folder even stored. CHECK THIS LINK FOR AN IMAGE http://dl.dropbox.com/u/10120092/Untitled.png[^] SHIVAM :)
As an alternative to Luc's suggestion you can combine the SystemDrive and HOMEPATH environment variables something like this:
public static string GetUserFolder()
{
return
Environment.GetEnvironmentVariable("SystemDrive") +
Environment.GetEnvironmentVariable("HOMEPATH");
}Edit: Just tested from explorer and using Process.Start and it appears to work with only the HOMEPATH variable too
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
There isn't a direct way, this however should give what you want:
string s=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
s=s.SubString(0, s.Length-8); // drop \Desktop:)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Shivam (/Luc); You can access this folder with the SpecialFolder
UserProfile
;String s = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
:)Dave Find Me On: Web|Facebook|Twitter|LinkedIn
Folding Stats: Team CodeProject
Only if using .NET framework V4
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Only if using .NET framework V4
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Hi, I've a university issued laptop with windows 7 on it. When I click on "windows" button it show my name as one of the system folder, but there is no path for it. It is above the "My Document" folder, in fact everyone in the university has same folder with their name [obviously]. I want to make an program that could extract the name of this folder when its it run on a particular system. And computer name, account name or name of Environment.getFolderName(SpecialFolder.Personal) all are different from this folder. I do not know where is this folder even stored. CHECK THIS LINK FOR AN IMAGE http://dl.dropbox.com/u/10120092/Untitled.png[^] SHIVAM :)
Having seen Dave's answer that it is available in v4, I've had a look at how it's done using reflector and these two functions combined perform the same task and will work with earlier .NET versions so long as it's run on a platform that supports it - if not, a PlatformNotSupportedException will be raised.
public const int UserProfile = 40;
[DllImport("shfolder.dll", CharSet = CharSet.Auto)]
private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);public static string GetUserPath()
{
StringBuilder resultBuilder = new StringBuilder(260);
int num = SHGetFolderPath(IntPtr.Zero, UserProfile, IntPtr.Zero, 0, resultBuilder);
if (num == -2146233031)
throw new PlatformNotSupportedException();
return resultBuilder.ToString();
}Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Well the OP did not make reference to any .Net version, i don't know what he is using :) [at least he knows now it exists in later frameworks!)
Dave Find Me On: Web|Facebook|Twitter|LinkedIn
Folding Stats: Team CodeProject
True. I've had a look in reflector and it's easy enough to do this in previous versions - see my other answer[^] below
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
There isn't a direct way, this however should give what you want:
string s=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
s=s.SubString(0, s.Length-8); // drop \Desktop:)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Hi, nothing seems to work. I tried your way, it only shows some other folder on my desktop.
-
Hi, nothing seems to work. I tried your way, it only shows some other folder on my desktop.
try to be specific. What have you tried and what was the outcome? What operating system, which .NET version, what exactly do you get, what exactly would you like to get. It works for me! :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
try to be specific. What have you tried and what was the outcome? What operating system, which .NET version, what exactly do you get, what exactly would you like to get. It works for me! :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
I tried your code. I get "C:\Users\100xxxxxx\Desktop" where 100xxxxxx is my student ID. I'm using .Net 3.5 but I do have .Net 4 with visual studios 10. Not why it is not working for me.. S
-
I tried your code. I get "C:\Users\100xxxxxx\Desktop" where 100xxxxxx is my student ID. I'm using .Net 3.5 but I do have .Net 4 with visual studios 10. Not why it is not working for me.. S
And is there a folder somewhere that holds your actual name instead of your student ID? All GetFolderPath() results are actual folder names. When a user account gets created, the user name displayed is also used as the folder name; it is however possible to change one without changing the other, as explained e.g. here[^]. I guess that is what happened to the student accounts: first create them numerically, then modify their display names. The display name of the current user should be available through
Environment.UserName
:)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
And is there a folder somewhere that holds your actual name instead of your student ID? All GetFolderPath() results are actual folder names. When a user account gets created, the user name displayed is also used as the folder name; it is however possible to change one without changing the other, as explained e.g. here[^]. I guess that is what happened to the student accounts: first create them numerically, then modify their display names. The display name of the current user should be available through
Environment.UserName
:)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Very strange but I do not see this folder anywhere on my computer. It shows when I press windows button or when I'm choosing or opening some file. I tried
Environment.UserName
but it is showing my student ID [LOL]. Anyways thanks, I checked your link, probably this what happening in my case too. DAM IT, but there has to be some folder which starts with my name. Shivam -
Very strange but I do not see this folder anywhere on my computer. It shows when I press windows button or when I'm choosing or opening some file. I tried
Environment.UserName
but it is showing my student ID [LOL]. Anyways thanks, I checked your link, probably this what happening in my case too. DAM IT, but there has to be some folder which starts with my name. ShivamYou're welcome. BTW: Most of the username/foldername magic is hidden in the registry, as the article explains. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
You're welcome. BTW: Most of the username/foldername magic is hidden in the registry, as the article explains. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
YUPPII. I solved the problem. I searched my name inside the registry and found that everyone is school has same registry key to that their name as registry value..so in my program I'm reading the registry value from this registry key..and it is working fine..thanks everyone again.. SHIVAM :)