1

I used below code to get the user's AppData folder -

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 

But what I got is "C:\Users\(users)\AppData\Roaming". Is there a way to only get "C:\Users\(users)\AppData"?

atiyar
  • 7,762
  • 6
  • 34
  • 75
Alex Huang
  • 13
  • 3
  • Can you please describe why you want this information? I'm not sure if there is any guarantee that the Roaming, Local and LocalLow folders are always sibling folders. – Klaus Gütter Mar 05 '21 at 06:08
  • There is an application that installed under C:\Users\(users)\AppData\LocalLow and I would like to create an update application for it. – Alex Huang Mar 10 '21 at 15:23
  • You should have included this information in your question. The answer is here already on StackOverflow: [Detect the location of AppData\LocalLow](https://stackoverflow.com/questions/4494290/detect-the-location-of-appdata-locallow) – Klaus Gütter Mar 10 '21 at 15:44
  • I had checked that before. As the answer is too much complicated, I gived up to use LocalLow directory. I think it would be easy to just append"LocalLow" string to the AppData. – Alex Huang Mar 10 '21 at 16:35
  • Please, for your next question, share what what you already have researched and why the results were not suitable for you. This saves people wanting to help you the time to repeat what you already did. And "too complicated": I would immediately choose the slightly more complicated, but correct solution over a solution that might work in 99% of the cases but fails in 1%. – Klaus Gütter Mar 10 '21 at 16:47

2 Answers2

2

First of all, accessing that folder directly is probably not a good idea unless Microsoft has published an API to retrieve its location. This means that there are no guarantees that this folder will even exist.

If you for some reason really want to retrieve this folder, you could probably do something along the lines of

Directory.GetParent(
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))

Then to verify, you could also retrieve e.g.

Directory.GetParent(
    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))

If the two are the same, it is likely the folder you want to find.

But again, it is probably a good idea to question the motivation on why you need this path in the first place.

Krumelur
  • 31,081
  • 7
  • 77
  • 119
-1

Is this what you are looking for

first get the user name from Environment object.

string userName = Environment.UserName;

then, use that User Name for generating the path.

string path = $"C:\\Users\\{userName}\\AppData";

Paramjot Singh
  • 627
  • 4
  • 8
  • 1
    That will result in a path that doesn't exist in many cases – Anon Coward Mar 05 '21 at 06:38
  • Did you mean if we are executing this code on Linux, because "path" string is structured according to windows directory pattern. – Paramjot Singh Mar 05 '21 at 07:05
  • 2
    It might not be on the C drive, it might not be under `C:\Users`, it might not be named the same as the username. Just because it is on your machine doesn't mean it's at all like that elsewhere. – Anon Coward Mar 05 '21 at 16:19