18

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

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Jeb
  • 3,689
  • 5
  • 28
  • 45
  • 1
    As a side note, I recommend using [Path.Combine](http://msdn.microsoft.com/en-us/library/system.io.path.combine.aspx) as oppose to concatenation for directories. – Brad Christie Jan 10 '11 at 18:08
  • Are you concerned with backwards compatibility with Windows XP? The %public% environment variable seems to have been introduced with Vista; WinXP has an %allusersprofile% environment variable that points to %systemdrive%\Documents and Settings\All Users but %public% is undefined. – 48klocs Jan 10 '11 at 18:12
  • Thanks for the info; No this is purely for Vista/7. – Jeb Jan 11 '11 at 09:33

6 Answers6

21

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;
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • Thanks for the info. Unfortunately my project is currently targeting 3.5 of the .Net framework which does not unclude the CommonDocuments enumeration member (we want to keep changes to a minimum between our old software and the new which is compatible with Windows 7). – Jeb Jan 11 '11 at 09:43
18

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Stoggy
  • 219
  • 2
  • 5
  • 3
    Not sure why this was marked as the answer or even up voted when it is wrong. CommonDesktopDirectory is not a valid enum for .Net 3.5 which is what is used for VS2008, which is how the question is tagged. – Steve Aug 25 '15 at 15:36
  • Clearly question was to find the directory " C/Users/Public " – Ammar Shaukat Jan 31 '19 at 09:42
6

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
Peter O.
  • 32,158
  • 14
  • 82
  • 96
dgp
  • 185
  • 1
  • 9
4

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")
Ben M
  • 22,262
  • 3
  • 67
  • 71
  • Thanks for the information. Yes this is data which can be accessed by all users, but it should also be visible to them in windows explorer, i.e. they can double click and open these files directly, hence C:\ProgramData in my opinion is not suitable since this is a hidden folder. – Jeb Jan 11 '11 at 09:36
  • 2
    Note another potential problem ProgramData folder has is limited privileges, eg http://stackoverflow.com/questions/22107812/privileges-owner-issue-when-writing-in-c-programdata – DAG Mar 11 '16 at 14:52
4

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)
Pauli Østerø
  • 6,878
  • 2
  • 31
  • 48
2

You can get all these %wildcard% literals by looking into

Windows->Start-->regedit-->

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders

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);
}
Jenna Leaf
  • 2,255
  • 21
  • 29