如何更改 Application.exe.config 文件在 .net 中的位置 [英] How to change the location of the Application.exe.config file in .net

查看:19
本文介绍了如何更改 Application.exe.config 文件在 .net 中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible in vb.net/C# to change where the application.exe.config file is located?

这与 this 问题,正如您在我的代码中看到的,我已经尝试过这种方法,但它对我不起作用.

This is not a duplicate of this question, as you can see in my code I tried this method already, it didn't work for me.

我想用它来实现的是动态创建这条路径.

The thing I want to achieve with this is to make this path dynamically.

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:TempAppConfig.exe.config")
        Dim config As Configuration = ConfigurationManager.OpenExeConfiguration("C:TempAppConfig.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 Framework 内部管理的.

You can't change the app config file location since the behavior is managed by the .NET Framework internally.

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

But you can create your own configuration file and manage parameters with a hand-made singleton class that can be serialized in xml or binary format where you want to put it.

示例:

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程序集引用.

It needs to add System.Runtime.Serialization assembly reference in the project file.

这篇关于如何更改 Application.exe.config 文件在 .net 中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆