What is the best way to get this folder path programmatically :
Windows\system32\config\systemprofile\AppData\Local
?
Asked
Active
Viewed 9,148 times
3

Soner Gönül
- 97,193
- 102
- 206
- 364

Andrei Karcheuski
- 3,116
- 3
- 38
- 39
-
%systemroot% will get you to the windows folder...but as far as my research (done just now), there is no direct shortcut like we have with %appdata% for example – Rohan Büchner Dec 03 '14 at 12:19
-
5I believe this is the AppData path for the SYSTEM account. So if you're running as the SYSTEM account, you should be able to use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) - though I haven't tested this. If you're not running as the SYSTEM account, you probably have no business there. – Joe Dec 03 '14 at 12:21
4 Answers
0
Sample code
HttpContext.Current.Server.MapPath();
System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;

SUNIL DHAPPADHULE
- 2,755
- 17
- 32
-1
I can't write comments sorry. What are you using the path for? Where is the application stored? This will get you there??
string path = "C:\\Windows\system32\config\systemprofile\AppData\Local";
Provided C: is the name of the drive.
You really need to expand on your question a little bit.
Is this a duplicate question??

Community
- 1
- 1
-
4
-
Yes, I can, but I thought it might be a solution in terms of clean code to get this speciacial folder path – Andrei Karcheuski Dec 03 '14 at 12:19
-
1@SonerGönül No I don't. This is programmatic. I think what the OP is looking for is probably closer to what Rohan is stating above. It depends on how the OP is accessing it. If his app is there it's `System.IO.Path.Combine(appPath, etc.);` If he's accessing it directly from the program installed elsewhere he'll have to add more of the path. How is the OP accessing it. That's the question. Like I said. I can't comment so I guess @downvoters rule for the moment. Just trying to help. – Dec 03 '14 at 12:25
-
1@AndreiKarcheuski Can you add any more detail to your question? Please see my response above to Soner – Dec 03 '14 at 12:26
-
1@FrankPytel I just working on logging in c# application, I've got a task where I need to store log files in that folder. So I just looking how to get that folder path programmatically, the application can be installed on different OS. – Andrei Karcheuski Dec 03 '14 at 12:34
-1
I think you can use Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData
In my project like this
string configPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MyProject", "SERVICER", "config.ini");
It get Application Data Directory for all user ( contain System, Service, Guest,... ). I use it for save config of Service!!

Henry Trần
- 1,286
- 12
- 14
-
Wrong. On my system, `CommonApplicationData` returns `C:\ProgramData`. – Anton Shepelev Apr 24 '19 at 17:35
-
Sorry for it's wrong for your problem !! But I don't know what you want !! Why do you need to get the link like that ?? To store data shared between accounts, or systemprofile will be replaced in the context of the user ?? If for both of the above, the Environment.GetFolderPath method will give you quite similar results. But if you really want to save to the `Windows\system32` directory, consider the option below: `Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "config", Environment.UserName, "AppData", "Local");` – Henry Trần May 02 '19 at 07:29
-
I need programmatically to create the directory `C:\Windows\System32\config\systemprofile\Desktop` (in case of 32-bit arhitecture) and `C:\Windows\SysWOW64\config\systemprofile\Desktop` (in case of 64-bit arhitecture) if it does not exist, in order to use *MS Office Interop* from a *Windows* service. – Anton Shepelev May 03 '19 at 09:55
-
Sorry for not understanding your intentions from the beginning, this is how I think you can use: `Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "config", "systemprofile", "AppData", "Local");` – Henry Trần May 07 '19 at 01:08
-
1`Environment.GetFolderPath(Environment.SpecialFolder.SystemX86)` will automatically give you the result of `system32` or `syswow64` depending on the executable computer. – Henry Trần May 07 '19 at 01:11
-
Thanks for the information, *Trịnh*. You could post an answer with `SpecialFolder.SystemX86`! Is this enum member misnamed? – Anton Shepelev May 07 '19 at 15:24
-
`// Summary: // The Windows System folder. Added in the .NET Framework 4. SystemX86 = 41,` This is a summary of this member. I think maybe you are not using dotnet 4 or higher !! If so, unfortunately, I think this is the easiest way to get the results you want above. – Henry Trần May 08 '19 at 04:46
-
1Sadly, simple ways are only in dotnet 4 or higher. If you still want to achieve the goal, you can refer to the following, as far as I know it will work on most windows systems but I have not had the opportunity to test them on a lower-end computer! [https://gist.github.com/DK189/11399b58985a1023f01d50118c23014a](Source code) – Henry Trần May 08 '19 at 05:11
-
Thanks for the snippet, Trinh. Notice that the *Markdown* link formatting is broken. Here is the [right link](gist.github.com/DK189/11399b58985a1023f01d50118c23014a). By the way, you can test for 64 vs 32 bit architecture by checking `IntPtr.Size`. – Anton Shepelev May 08 '19 at 11:14
-
Sorry for the markdown syntax! Regarding `IntPtr.Size` this has a disadvantage: if the application is built with the x86 flag, it will always have a value of 4, so in your request, using `IsWow64Process` in `kernel32` will always more accurate! – Henry Trần May 09 '19 at 03:28
-
If the application is build for x86, it executes in the x86 environment and I need `Windows\system32\` in that case. – Anton Shepelev May 09 '19 at 09:39
-
Well,... if you run x86 app on x64 environment, it always return `Windows\system32`,.. and on x64 environment, `systemprofile` only exists in `SysWOW64` folder!! – Henry Trần May 14 '19 at 03:49
-2
You can get the path this way:
Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData)

Nacharya
- 229
- 2
- 8
-
Wrong. When called from a Windows service, `LocalApplicationData` returns `C:\Users\anton\AppData\Local` on my system. – Anton Shepelev Apr 24 '19 at 17:38