簡體   English   中英

在 Windows 窗體 C# 應用程序的配置文件中設置值

[英]Set a value in a Configuration File in a Windows Forms C# Application

使用 Microsoft Visual Studio 社區 2019。

如何在配置文件中設置一個值(路徑),以便可以更新它,然后使用更新后的值重新啟動應用程序(無需重新編譯應用程序)?

這是一個我真的不知道的遺留項目。 最重要的要求是能夠更改此路徑而無需重新編譯應用程序。 這是我迄今為止嘗試過的:

預備步驟:

檢查它是什么類型的項目。 基於這個問題,我檢查.csproj文件是否包含:

<OutputType>WinExe</OutputType>

所以,據我所知,該項目是一個WinForms Application

另外,我嘗試搜索在.csproj文件中找到的 GUID:

<ProjectGuid>{B1DD5159-A8E8-4DBE-964F-DB9679F60192}</ProjectGuid>

但是對那個 GUID 的搜索沒有給出結果,也沒有列在這兩個 GUID 列表中:

注意:問題的核心是基於這個項目是一個 WinForms 應用程序的假設。 請讓我知道這是否是查找該項目類型的錯誤方法,或者甚至更好地指出正確的方法。


第一步:

要編輯配置文件,請轉到解決方案資源管理器,在其中我找不到任何.config文件,但是如果我檢查該項目的文件(通過 Windows 資源管理器),我找到了一個app.config文件。

我在 Visual Studio 之外更新了這個文件(稍后我會回來),並添加一個test屬性。 這個修改是按照文件中的結構完成的,沒有外部源來做這個修改。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="LF10Demo.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <LF10Demo.Properties.Settings>
            <setting name="AutoStepping" serializeAs="String">
                <value>True</value>
            </setting>
            ...
            <setting name="test" serializeAs="String">
                <value>C:\Temp</value>
            </setting>
        </LF10Demo.Properties.Settings>
    </userSettings>
</configuration>

然后,在代碼中嘗試獲取值:

var appSettings = System.Configuration.ConfigurationManager.AppSettings;
string value1 = System.Configuration.ConfigurationManager.AppSettings["test"];
string value4 = System.Configuration.ConfigurationSettings.AppSettings.Get("test");

這些都沒有帶來存儲在app.config文件中的值,實際上所有的都帶來了null

然后,在 Windows 窗體 C# 應用程序中按照此后配置文件更新app.config文件

<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="test" value="C:\Temp" />
      </appSettings>
    </configuration>

這也不起作用。

第二步

回到對app.config的修改,去掉這個修改,添加一個不同的配置文件(畢竟app.config在解決方案資源管理器中是不可見的)。 這包括在這篇與 Windows Forms C# 應用程序中的配置文件相關的app1.config之后添加一個新的配置文件app1.config這個app1.config and reading from a Config file

因此,在添加新的配置文件后,我所有帶有System.Configuration.ConfigurationManager.AppSettings apptemps 都會帶來null

另外,當添加這個新的配置文件時,如果我選擇app.config顯示一條消息說

名為“App.config”的文件已經存在...

畢竟,該項目正在(以某種方式)找到該文件。

其他相關資料:

如果我在文件Settings.Designer.cs添加以下內容:

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("C:\\Temp")]
public string test
{
    get {
        return ((string)(this["test"]));
    }
    set {
        this["test"] = value;
    }
}

然后使用這一行:

string value8 = Properties.Settings.Default.test;

是可以獲取到值的,但是這不滿足不需要重新編譯應用程序就可以改變路徑的要求。

其他相關鏈接:

這是與向 Visual Studios 項目添加配置文件相關的其他鏈接,我也嘗試了其中的一些解決方案,但沒有獲得預期的結果:

查看您的 bin 目錄,您應該會看到一個名為 YourProjectName.exe.config 的文件,其中包含一種 XML 配置。 編譯項目時,app.config 中的任何內容都會復制到此處。 您可以按如下方式更改代碼中的值:

static void UpdateAppSettings(string key, string value)
        {
            var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var settings = configFile.AppSettings.Settings;
            settings[key].Value = value;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM