简体   繁体   English

C#在应用程序配置文件中设置appdata或任何特殊文件夹路径

[英]C# set appdata or any special folder path in app config file

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. 我试图找到一种将appdata路径用作我的environment.currentdurectory的方法,这意味着我想从appdata文件夹运行c#应用程序,并且在我的项目中,我始终使用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... 我找不到一种方法可以在app config中设置appdata路径,然后再用appdata路径替换environment.curentdirectory ...

PS:. PS :。

1) I want to set my programs data path as appdata 1)我想将我的程序数据路径设置为appdata

2) my project code is set to use environment.currentdirectory 2)我的项目代码设置为使用environment.currentdirectory

3) I don't want to replace environment.currentdirectory by going each line by line 3)我不想通过逐行替换替换environment.currentdirectory

Target .Net framework 4 目标.Net框架4

Have you tried doing this via the app domain. 您是否尝试通过应用程序域执行此操作。

See: 看到:

https://docs.microsoft.com/en-us/dotnet/api/system.appdomain.basedirectory?view=netframework-4.7.2 https://docs.microsoft.com/zh-cn/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);

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. 并使用Environment.specialfolder方法。

Otherwise the answer posted above is given by MSDN itself and obviously works but too large code 否则,上面发布的答案由MSDN本身给出,显然可以工作,但是代码太大

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM