Opening folders with C# is easy, sometimes you just want the path to the folder so you can read, write or access files but sometimes you want to open the folder for the user to see visually.
To open the Program Files folder for the user you would use the following code:
//Use the Environment.SpecialFolder.ProgramFiles
//to get the path to Program Files in case it
//isn't stored on the C: drive.
string path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
//Get the Windows directory
string windir = Environment.GetEnvironmentVariable("WINDIR");
System.Diagnostics.Process prc = new System.Diagnostics.Process();
//Use the Windows Explorer to view the Program Files directory
prc.StartInfo.FileName = windir + @".exe";
prc.StartInfo.Arguments = path;
prc.Start();
A couple notes about the preceeding code:
Explorer.exe is located in the Windows directory which on my computer is located in C:\WINDOWS
The first line gets the path to the Program Files directory:
The second line gets the Windows directory using the System Environment variable. You can look at where this is set
1. Go to the Desktop and right click on My Computer, go to properties.
2. Click the Advanced tab.
3. On the Advanced tab click on the Environment Variables button
4. It is located in the System Variables section:
Then using the System.Diagnostics.Process namespace the code creates a process to start Windows Explorer from the Windows directory and passes the path to Program Files as an argument:
You can essentially replace the Program Files directory with any directory your computer to open up that directory.
Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.System)).FullName
Theme design by Jelle Druyts
Pick a theme: BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2 BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Steven Rockarts
E-mail