Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

c# - How to change the location of the Application.exe.config file in .net

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

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...
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

Example:

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; }
}

The test:

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."); 
  }
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...