Is it safe to programmatically reference the public folder through:
Directory = System.Environment.GetEnvironmentVariable(\"public\")+\"MyCompanyName\" // et
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.
Have you looked at this ?
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
Specifies enumerated constants used to retrieve directory paths to system special folders.
Ie
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
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;
You can get all these %wildcard% literals by looking into
Windows->Start-->regedit-->
Then, you perform
using System;
string path2Downloads = Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\Downloads");
string path2Music = Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\Music");
... and, so on .... and to test:
using System.IO;
string[] files = { "" };
if (Directory.Exists(path2Music)) {
files = Directory.GetFiles(path2Music);
}
If you want a place to put application-specific data that can accessed by all users, use as a base:
Environment.GetFolderPath(SpecialFolder.CommonApplicationData)
Also, consider using Path.Combine
to combine elements to form a new path:
Path.Combine(
Environment.GetFolderPath(SpecialFolder.CommonApplicationData),
"MyCompanyName")
Notice that the Environment.SpecialFolder.CommonDesktopDirectory is only available in .NET 4.0. For my .NET 3.5 systems (Windows 7 or XP) I used the registry key for the Shell Folders. My code snippet is in VB.NET.
Private mRegShellPath="Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
Private mCommonDesktop = Nothing
' dgp rev 3/8/2012
Private ReadOnly Property CommonDesktop As String
Get
If mCommonDesktop Is Nothing Then
Dim RegKey As RegistryKey
Try
RegKey = Registry.LocalMachine.OpenSubKey(mRegShellPath, False)
mCommonDesktop = RegKey.GetValue("Common Desktop")
Catch ex As Exception
mCommonDesktop = ""
End Try
End If
Return mCommonDesktop
End Get
End Property