Get user root directory from windows service

I have a windows service written in C #. I need to add a file to each user directory. How can I find a way to start? I understand that this is really stupid, but this is what I am doing now:

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

I 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 returns the path to the System32 directory. I suppose this is because it works as a Windows service. The code that I am currently using for several tests that I have done. It seems that there must be a more intelligent (with an error) way for this path.

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

+4
source share
2 answers

I found this in the Win7 registry. XP has the same registry keys, but they have a "All Users" profile.

 [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" 
+1
source

Perhaps I did not understand you completely, but I think you just need to get the place you work from, and then get the root from it.

 Path.GetPathRoot(Assembly.GetEntryAssembly().Location); 

Update

special folders are what you need to explore. It is not so easy to do this because your question is not detailed enough. If the service works as a specific user or as a system, it matters. In addition, you did not indicate how it was used to make a difference. Is this a place to store configuration files against a temporary location to write vs files if someone else wants to get into a folder? All of this matters, so if you add more data, I will update the answer.

0
source

Source: https://habr.com/ru/post/1496012/


All Articles