【问题标题】:How to change the location of the Application.exe.config file in .net如何在 .net 中更改 Application.exe.config 文件的位置
【发布时间】:2020-02-01 19:02:10
【问题描述】:

是否可以在 vb.net/C# 中更改 application.exe.config 文件的位置?

这不是this 问题的不重复,正如您在我的代码中看到的那样,我已经尝试过这种方法,但它对我不起作用。

我想要实现的是动态地创建这条路径。

Public Class Form1
Public Sub New()
    Try
        'Two Methods to change the path of the application.exe.config file i tried, both dont work
        AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "C:\Temp\AppConfig.exe.config")
        Dim config As Configuration = ConfigurationManager.OpenExeConfiguration("C:\Temp\AppConfig.exe.config")

        InitializeComponent()

        'EntityFramework ---------------------------
        Dim db = New WfpModel 'DbContext --> MyBase.New("name=ConnectionMSSQL")
        gridWFP.DataSource = db.ADR.ToList()
        gridWFP.Refresh()

        'WebService ---------------------------
        Dim Client = New Netlogistik.ServiceClient
        Dim filter = New TRANSPORT_FILTER With {.ID = 0}
        gridNet.DataSource = Client.WfpNetTransport("myUserName", "myPassword", filter.GetTransportFilter)?.Tables("OUT")
        gridNet.Refresh()

    Catch ex As Exception
        'EntityFramework Result: System.InvalidOperationException:
        '               "No connection string named ConnectionMSSQL was found in the application configuration file."
        '
        'WebService Result: No default endpoint element was found that references the Netlogistic.IService 
        '               contract in the ServiceModel client configuration section. 
        '               This could be due to the following reasons: No configuration file was found for the application,
        '               Or no endpoint element matching the contract was found in the client element.
        MsgBox(ex.Message)
    End Try
End Sub...

【问题讨论】:

    标签: c# .net vb.net visual-studio configuration


    【解决方案1】:

    您无法更改应用配置文件的位置,因为该行为由 .NET Framework 内部管理。

    但是您可以创建自己的配置文件并使用手工制作的单例类管理参数,该类可以序列化为您想要放置的 xml 或二进制格式。

    例子:

    using System.Xml.Serialization;
    
    [Serializable]
    public class AppSettings
    {
      // The singleton
      static public AppSettings Instance { get; private set;  }
      static public string Filename { get; set; }
      static AppSettings()
      {
        Instance = new AppSettings();
      }
      // The persistence
      static public void Load()
      {
        if ( !File.Exists(Filename) )
          return;
        using ( FileStream fs = new FileStream(Filename, FileMode.Open, FileAccess.Read) )
          Instance = (AppSettings)new XmlSerializer(typeof(AppSettings)).Deserialize(fs);
      }
      static public void Save()
      {
        using ( FileStream fs = new FileStream(Filename, FileMode.Create, FileAccess.Write) )
          new XmlSerializer(Instance.GetType()).Serialize(fs, Instance);
      }
      // The settings
      public bool IsFirstStartup { get; set; } = true;
      public string ExportPath { get; set; }
    }
    

    测试:

    static void Test()
    {
      AppSettings.Filename = "c:\\Test\\AppSettings.xml";
      AppSettings.Load();
      if ( AppSettings.Instance.IsFirstStartup )
      {
        AppSettings.Instance.IsFirstStartup = false;
        AppSettings.Instance.ExportPath = "c:\\Test\\Export";
        AppSettings.Save();
        Console.WriteLine("App initialized.");
      }
      else
      {
        Console.WriteLine("Welcome back."); 
      }
    }
    

    需要在项目文件中添加System.Runtime.Serialization的程序集引用。

    【讨论】:

    • 我是 C# 新手,希望能够将配置设置保存并加载到文件中。我已经使用应用程序设置设置了内置保存,它运行良好,但我需要更多。您的代码看起来可能对我有用,但我不确定我应该把它放在哪里以及如何调用这些函数。你能帮忙吗?非常感谢。
    • 您可以使用标准的UserDataFolderPath,例如stackoverflow.com/questions/58202091/…中公开的内容
    猜你喜欢
    • 1970-01-01
    • 2011-12-05
    • 2021-04-28
    • 2010-12-20
    • 2011-01-31
    • 1970-01-01
    • 2016-07-08
    • 2015-11-05
    • 1970-01-01
    相关资源
    最近更新 更多