Getting the path to the user directory

How to get the path to the Users folder from a Windows service on MS Vista? I think about the path of the C: \ Users directory , but it may be a different location depending on the localization of the system.

+5
source share
5 answers

See Environment.SpecialFolder Enumeration for example

 Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory);

Adjust the desired folder. However, when reading another post found here , it looks like you might need to manipulate the line a bit if you want exactly c: \ users instead of c: \ users \ public, for example.

+11
source

System.Environment.SpecialFolder , , " " ..

UserProfile SpecialFolder, "".

string userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
+7

, @Neil SHGetKnownFolderPath() FOLDERID_UserProfiles. # . :

using System;
using System.Runtime.InteropServices;

namespace SOExample
{
    public class Main
    {
        [DllImport("shell32.dll")]
        static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr pszPath);

        private static string getUserProfilesPath()
        {
            // https://msdn.microsoft.com/en-us/library/windows/desktop/dd378457(v=vs.85).aspx#folderid_userprofiles
            Guid UserProfilesGuid = new Guid("0762D272-C50A-4BB0-A382-697DCD729B80");
            IntPtr pPath;
            SHGetKnownFolderPath(UserProfilesGuid, 0, IntPtr.Zero, out pPath);

            string path = System.Runtime.InteropServices.Marshal.PtrToStringUni(pPath);
            System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pPath);
            return path;
        }

        static void Main(string[] args)
        {
            string path = getUserProfilesPath(); // C:\Users
        }
    }
}
+2

, .NET, C (++)

SHGetKnownFolderPath(FOLDERID_UserProfiles, ...)
+1

System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)

0

All Articles