1

I was trying to find a way to use appdata path as my environment.currentdurectory which means I want to run the c# application from appdata folder and in my project I always used environment.currentdirectory.

I couldn't find a way that I can set appdata path in app config and later replace environment.curentdirectory by appdata path...

PS:.

1) I want to set my programs data path as appdata

2) my project code is set to use environment.currentdirectory

3) I don't want to replace environment.currentdirectory by going each line by line

Target .Net framework 4

  • You should only rely on current directory if you really mean to work with the current directory. If you want a special folder work with the special folder – Sir Rufo Jan 07 '19 at 08:33
  • I actually made my project like any other amateur Dev and I found that the current directory restricts access which will cause the issue so I want to move to the special folder but I don't want to change codes. – user10485400 Jan 07 '19 at 09:08
  • Using the current directory is fragile if you have to rely on a specific directory because it can change during runtime (even unexpected) and can cause some unwanted side effects. If you want to get rid of these (potential) side effects you have to change your code. Period. – Sir Rufo Jan 07 '19 at 09:23

2 Answers2

0

Have you tried doing this via the app domain.

See:

https://learn.microsoft.com/en-us/dotnet/api/system.appdomain.basedirectory?view=netframework-4.7.2

    // Create application domain setup information
    var domaininfo = new AppDomainSetup();
    domaininfo.ConfigurationFile = System.Environment.CurrentDirectory + 
                                   Path.DirectorySeparatorChar +
                                   "ADSetup.exe.config";
    domaininfo.ApplicationBase = System.Environment.CurrentDirectory;

    //Create evidence for the new appdomain from evidence of the current application domain
    Evidence adEvidence = AppDomain.CurrentDomain.Evidence;

    // Create appdomain
    AppDomain domain = AppDomain.CreateDomain("Domain2", adEvidence, domaininfo);

    // Display application domain information.
    Console.WriteLine("Host domain: " + AppDomain.CurrentDomain.FriendlyName);
    Console.WriteLine("Child domain: " + domain.FriendlyName);
    Console.WriteLine();
    Console.WriteLine("Configuration file: " + domain.SetupInformation.ConfigurationFile);
    Console.WriteLine("Application Base Directory: " + domain.BaseDirectory);

    AppDomain.Unload(domain);
Michael Ceber
  • 2,304
  • 1
  • 11
  • 14
  • No. I didn't know that it can be done this way. I will try this and surely this one is a bit different but looks great. Thanks! – user10485400 Jan 07 '19 at 09:17
  • @user10485400 When you have found out - please come back here and mark the answer as correct or comment otherwise. – LosManos Jan 07 '19 at 14:04
0

So, I found that we can not get app data path from the config file directly unless we set any.

The best use se would be just get the app data path by

Environment.specialfolder method and use that.

Otherwise the answer posted above is given by MSDN itself and obviously works but too large code