How can I get the current user directory?

前端 未结 9 1002
说谎
说谎 2020-11-27 13:33

Using this:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

I get this output:

\"C:\\\\Documents and S         


        
相关标签:
9条回答
  • 2020-11-27 13:53
    $env:USERPROFILE = "C:\\Documents and Settings\\[USER]\\"
    
    0 讨论(0)
  • 2020-11-27 14:00
    Environment.GetEnvironmentVariable("userprofile")
    

    Trying to navigate up from a named SpecialFolder is prone for problems. There are plenty of reasons that the folders won't be where you expect them - users can move them on their own, GPO can move them, folder redirection to UNC paths, etc.

    Using the environment variable for the userprofile should reflect any of those possible issues.

    0 讨论(0)
  • you can use the following code:

    if(Platform.Equals("WinCE"))
    {
        m_CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
    }
    else if(Platform.Equals("Win32NT"))
    {
        m_CurrentPath = Directory.GetCurrentDirectory();
    }
    

    more information see: Get Current Directory Path in both WinXP and WinCE with C#

    0 讨论(0)
  • 2020-11-27 14:02

    May be this will be a good solution: taking in account whether this is Vista/Win7 or XP and without using environment variables:

    string path = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName;
    if ( Environment.OSVersion.Version.Major >= 6 ) {
        path = Directory.GetParent(path).ToString();
    }
    

    Though using the environment variable is much more clear.

    0 讨论(0)
  • 2020-11-27 14:06

    Try:

    System.IO.Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName/
    
    0 讨论(0)
  • 2020-11-27 14:09

    Try:

    System.Environment.GetEnvironmentVariable("USERPROFILE");
    

    Edit:

    If the version of .NET you are using is 4 or above, you can use the Environment.SpecialFolder enumeration:

    Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
    
    0 讨论(0)
提交回复
热议问题