Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更改.net中Application.exe.config文件的位置_C#_.net_Vb.net_Visual Studio_Configuration - Fatal编程技术网

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

C# 如何更改.net中Application.exe.config文件的位置,c#,.net,vb.net,visual-studio,configuration,C#,.net,Vb.net,Visual Studio,Configuration,在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

在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 Framework内部管理

但是,您可以创建自己的配置文件,并使用手工制作的singleton类管理参数,该类可以以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
,例如