views:

393

answers:

4

In Windows Vista, the special folder "Shared Documents" contains documents accessible by all the users in the machine. That folder was renamed to "Public Documents" in Windows 7.

How can I find its physical path from c#?

Note that Environment.GetFolderPath(Environment.SpecialFolder.xxx) doesn't have the folder I'm looking for.

+3  A: 

What about this?

[DllImport("shell32.dll")]
static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken,
   uint dwFlags, [Out] StringBuilder pszPath);

public string GetCommonDocumentsFolder()
{
    int SIDL_COMMON_DOCUMENTS = 0x002e;
    StringBuilder sb = new StringBuilder();
    SHGetFolderPath(IntPtr.Zero,SIDL_COMMON_DOCUMENTS,IntPtr.Zero,0x0000,sb);
    return SB.ToString();
}

Answer courtesy of, er..., expert-exchange that we all love to hate.

Ngu Soon Hui
+1  A: 
Path.Combine(Environment.GetEnvironmentVariable("PUBLIC"), "Documents");
cxfx
+1 for simplicity
Rubens Farias
This is a big fail and should not be marked as the answer. You should NOT hardcode names like "Documents".
Anders
@Anders "Documents" in this case is a system folder, what's a better alternative, other than using SpecialFolder CommonDocuments which isn't available until .NET 4.0?
cxfx
@cxfx: PInvoke one of the SHGet*Folder api's like Ngu Soon Hui's answer
Anders
+4  A: 

The SpecialFolder enum has had a large, and long overdue, update in .NET 4 - one of the new additions is the CommonDocuments member.

LukeH
A: 

Look in ShlObj.h for more CSIDLs interestingly enough SHGetFolderPath has been depricated... See http://msdn.microsoft.com/en-us/library/bb762181(VS.85).aspx

Angus Connell