Is it safe to programmatically reference the public folder through:
Directory = System.Environment.GetEnvironmentVariable("public")+"MyCompanyName" // etc.
or is there a better way?
Again, what if someone deletes the environment variable for public, and is this safe to use for different language OSs?
This follows: How to install to the Public directory in Windows 7 from the VS 2010 deployment Setup Project
This seems a tad questionable, but it should work:
// This should give you something like C:\Users\Public\Documents
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);
var directory = new DirectoryInfo(documentsPath);
// Now this should give you something like C:\Users\Public
string commonPath = directory.Parent.FullName;
It depends on what you want to achieve.
There is a enum called SpecialFolder
. You can use it to get the Path to some Directories.
For Example:
System.Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)
points to "C:\Users\Public\Desktop".
IMHO, your way isn't wrong, though i would do some Exception Handling in case the EnvVar is really missing. Also you could use the ENUM with "CommonDesktopDirectory" and get rid of the "\Desktop" part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With