How to reference the C:\Users\Public directory programmatically in C#

前端 未结 6 912
日久生厌
日久生厌 2021-01-04 00:33

Is it safe to programmatically reference the public folder through:

Directory = System.Environment.GetEnvironmentVariable(\"public\")+\"MyCompanyName\" // et         


        
6条回答
  •  情话喂你
    2021-01-04 00:44

    It depends on what you want to achieve. There is a enum called SpecialFolder. You can use it to get the Path to some Directories. For Example:

    System.Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)
    

    points to "C:\Users\Public\Desktop".

    IMHO, your way isn't wrong, though i would do some Exception Handling in case the EnvVar is really missing. Also you could use the ENUM with "CommonDesktopDirectory" and get rid of the "\Desktop" part.

提交回复
热议问题