Jamisco
Jamisco

Reputation: 1862

C# Windows Service Enviroment.GetFolderPath() returning empty string

The question is pretty straight forward, I am making a Windows Service Program and the enviroment.getfolderpath isnt working.

Here is the code I have

string savePath = AppDomain.CurrentDomain.BaseDirectory; // this works
string savePath2 = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // this returns an empty string...but why?

Upvotes: 0

Views: 1209

Answers (1)

Akshay G
Akshay G

Reputation: 2280

The documentation says

The path to the specified system special folder, if that folder physically exists on your computer; otherwise, an empty string ("").A folder will not physically exist if the operating system did not create it, the existing folder was deleted, or the folder is a virtual directory, such as My Computer, which does not correspond to a physical path.

When running the Service as a Local System it doesn't run with any specific user permissions. Hence the GetFolderPath is returning empty because it is not able to recognize the path Desktop for LocalSystem.

You can either use Environment.SpecialFolder.CommonDesktopDirectory which will give C:\Users\Public\Desktop or run the service with a specific user (in my case it's sampleuser) which will give the output as C:\Users\sampleuser\Desktop

enter image description here

Upvotes: 1

Related Questions