3

I have a windows application. In that i have retrieved appdata using environment variable. So it gives me following path

c:\document and settings\current user name\application data.

But when I retrieve the appdata path from windows service using environment variable i get following path

c:\windows\ServiceProfiles\LocalService\AppData\Local

so this appdata path is different from appdata path that i got from windows application environments variable appdata path.

I m running windows service under local profile. I know that if i change service profile to run under user then service appdata path and windows application appdata path matches but service prompts for username and password.

so my question is how to get user appdata path from service by running service under local profile without prompting for username and password?

Mohit Shah
  • 152
  • 3
  • 13
  • are you sure that, even if you manage to get, you can read/write from it? – Felice Pollano Jun 04 '13 at 15:54
  • Yes I m sure that if I get the path I can read and write to it. Actually from windows application I m creating my own folder in user appdata. Now from windows service I need to read and write data from or to that created folder under users appdata without running service under user profile. Because if runned under user profile I get exact appdata path but it prompts for username and password which I want to avoid. If runned under local profile it does not prompt for credentials but appdata path is different. – Mohit Shah Jun 04 '13 at 16:15
  • I solved the issue. After doing a lot of googling and R&D I found out that is not possible to get user appdata path from windows service by running service under profile "LocalSystem". So I used Environment.SpecialFolder.CommomAppData which gives me app data path C:\ProgramData when run on windows 7 and when used same thing in windows service, it also gived the same path and i also ran the service under profile "LocalSystem" so it did not prompt me for credentials. So this solved my problem. – Mohit Shah Jun 05 '13 at 16:36
  • You should create an Answer post to your own Question and then accept it instead of writing a comment. This helps other see that it is solved and easily see the solution. – user1651105 Mar 06 '14 at 07:55

2 Answers2

4

I have also encountered in that problem and looked at your question but i at first sight didnot find an answer in it.

This is the Mohit shah Answer

"I found out that is not possible to get user appdata path from windows service by running service under profile "LocalSystem". So I used Environment.SpecialFolder.CommomAppData which gives me app data path C:\ProgramData when run on windows 7 and when used same thing in windows service, it also gived the same path and i also ran the service under profile "LocalSystem" so it did not prompt me for credentials. So this solved my problem."

@Mohit Shah Please mark this is as answer so that other can take help from that.

ovais
  • 339
  • 2
  • 13
0

Windows service will always runs on SYSTEM level and hence it wont able to access user specific folder. Either as @ovais suggested you can store user data inside program data folder or you can use following approach.

You can use Windows management API's to get the current windows user name.Usually remaining path will be constant and hence you can construct remaining path.

Say for example, data is stored inside - "C:\Users\xyzUser\appdata\roaming..."

Only thing which is not constant here is "xyzUser" and "C"(User can install in different drives).

public static string GetWindowsUserAccountName()
{
string userName = string.Empty;
ManagementScope ms = new ManagementScope("\\\\.\\root\\cimv2");
ObjectQuery query = new ObjectQuery("select * from win32_computersystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(ms, query);

foreach (ManagementObject mo in searcher?.Get())
{
userName = mo["username"]?.ToString();
}
userName = userName?.Substring(userName.IndexOf(@"\") + 1);

return userName;
}

Drawback of this approach is, when you connected through remote connection, username will give you "NULL". So please be careful while using .

Windows folder you can get through following snippet.

public static string GetWindowsFolder()
{
string windowsFolder = string.Empty;
ManagementScope ms = new ManagementScope("\\\\.\\root\\cimv2");
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(ms, query);
foreach (ManagementObject m in searcher?.Get())
{
windowsFolder = m["WindowsDirectory"]?.ToString();
}
windowsFolder = windowsFolder.Substring(0, windowsFolder.IndexOf(@"\"));
return windowsFolder;
}
Praveen M
  • 443
  • 2
  • 11