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;
}