繁体   English   中英

如何更改 .net 中 Application.exe.config 文件的位置

[英]How to change the location of the Application.exe.config file in .net

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

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

我想用这个实现的事情是动态地制作这条路径。

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...

您无法更改应用程序配置文件的位置,因为该行为由 .NET 框架在内部进行管理。

但是您可以创建自己的配置文件并使用手工制作的 singleton class 可以序列化为 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程序集引用。

暂无
暂无

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

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