How to get current windows directory e.g. C:\ in C#

后端 未结 4 1159
清歌不尽
清歌不尽 2020-12-31 00:31

As the title suggests, how can you get the current OS drive, so you could add it in a string e.g.:

MessageBox.Show(C:\\ + \"My Documents\");
4条回答
  •  时光说笑
    2020-12-31 00:41

    You can use Environment.CurrentDirectory to get the current directory. Environment.SystemDirectory will give you the system folder (ie: C:\Windows\System32). Path.GetPathRoot will give you the root of the path:

    var rootOfCurrentPath = Path.GetPathRoot(Environment.CurrentDirectory);
    var driveWhereWindowsIsInstalled = Path.GetPathRoot(Environment.SystemDirectory);
    

提交回复
热议问题