Find windows folder programmatically in c#

后端 未结 4 647
甜味超标
甜味超标 2021-02-01 13:52

I am writing a program to kill and restart explorer but I don\'t want to hard code the location because some people install windows in different places (for example I found some

相关标签:
4条回答
  • 2021-02-01 14:18

    I would highly recommend the use of:

    Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.System))
    

    It does NOT require administrator rights and supports all versions of the .NET framework.

    0 讨论(0)
  • 2021-02-01 14:32

    To simply kill and restart Windows Explorer you wouldn't need the path to the system folder as this is already included in the PATH environment variable (unless the user messed with it).

    That short program will kill all explorer.exe instances and then restart explorer.exe:

    static void Main(string[] args)
    {
        foreach (Process process in Process.GetProcessesByName("explorer"))
        {
            if (!process.HasExited)
            {
                process.Kill();
            }
        }
        Process.Start("explorer.exe");
    }
    
    0 讨论(0)
  • 2021-02-01 14:34

    Environment.GetFolderPath( Environment.SpecialFolder.Windows ) will return the path to the Windows folder. Recommend this approach over the environment variable, because using an API that does exactly what we want (.NET 4.0 and above).

    0 讨论(0)
  • 2021-02-01 14:41

    http://msdn.microsoft.com/en-us/library/77zkk0b6.aspx

    Try these:

    Environment.GetEnvironmentVariable("SystemRoot")
    
    Environment.GetEnvironmentVariable("windir")
    
    0 讨论(0)
提交回复
热议问题