Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get user root directory from windows service

Tags:

c#

.net

windows

I have a Windows Service written in C#. I need to add a file to each user directory. How can I find the path to start in? I realize this is really dumb but this is what I'm currently doing:

  if (Directory.Exists("C:\\Users"))
  {
    path = "C:\\Users";
  }
  else if (Directory.Exists("C:\\Documents and Settings"))
  {
    path = "C:\\Documents and Settings";
  }

I've looked at the special folders: http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx

None of them seem to return what I need. For example, ApplicationData is returning the path to the System32 directory. I presume this is because it is running as a windows service. The code I'm currently using works for the few tests I've done. It just seems like there should be a more intelligent (error proof) way of getting this path.

Another thought...maybe there is a registry key that will give me what I'm looking for? Hmmm

like image 639
exvance Avatar asked Nov 12 '22 00:11

exvance


1 Answers

I found this under Win7 registry. XP looks to have the same registry keys, but the values have "All Users" profile within it.

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders]
"Common Desktop"="C:\\Users\\Public\\Desktop"
"Common Start Menu"="C:\\ProgramData\\Microsoft\\Windows\\Start Menu"
"CommonVideo"="C:\\Users\\Public\\Videos"
"CommonPictures"="C:\\Users\\Public\\Pictures"
"Common Programs"="C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs"
"CommonMusic"="C:\\Users\\Public\\Music"
"Common Administrative Tools"="C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Administrative Tools"
"Common Startup"="C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Startup"
"Common Documents"="C:\\Users\\Public\\Documents"
"OEM Links"="C:\\ProgramData\\OEM Links"
"Common Templates"="C:\\ProgramData\\Microsoft\\Windows\\Templates"
"Common AppData"="C:\\ProgramData"
like image 50
Chizl Avatar answered Nov 14 '22 21:11

Chizl