7

When I use the code

Properties.Settings.Default.Save();

to save my settings it writes the file to this path

%USERPROFILE%\AppData\Local\MyAppName\MyAppName.exe_Url_SomeWeirdCode\TheAppVersionNumber\user.config

For example

C:\Users\Username\AppData\Local\MyApp\MyApp.exe_Url_claumreyuxtgqul2vuc3couyu5tso2n0\1.0.0.0\user.config

How can I get the path to the app version folder (1.0.0.0) so I can write other stuff to it?

I am using .NET Framework 4.6.2 and Visual Studio 2017.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
ahmedpio
  • 132
  • 1
  • 10

2 Answers2

6

You should be able to retrieve the file path like this:

var level = ConfigurationUserLevel.PerUserRoamingAndLocal;
var configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(level);
var configurationFilePath = configuration.FilePath

To make this work, you need to add reference to System.Configuration.dll (right-click your WPF project, select Add, Reference... and check the box next to System.Configuration in the Assemblies/Framework page.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
-2

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

This returns a string that goes to AppData. Just add the rest of your file path on to the end of it.

Jackson Tarisa
  • 298
  • 2
  • 9
  • 1
    Honestly, I feel like *"Just add the rest of your file path on to the end of it"* is a pretty vague replacement for correctly adding something similar to `MyApp\MyApp.exe_Url_claumreyuxtgqul2vuc3couyu5tso2n0\1.0.0.0` and possibly maintaining it accross multiple version updates. – grek40 Feb 20 '18 at 09:14
  • 3
    the problem is the code after the appname.exe "claumreyuxtgqul2vuc3couyu5tso2n0" is unique for each user and or pc i can't just put an static value for it – ahmedpio Feb 20 '18 at 09:30
  • Oh I think I understand now, your trying to find a file which exists with an identifier that's unique. – Jackson Tarisa Feb 20 '18 at 09:34
  • @JacksonTarisa yes – ahmedpio Feb 20 '18 at 09:44