導航:首頁 > 文件管理 > wpf讀配置文件

wpf讀配置文件

發布時間:2023-03-17 21:27:09

① 在WPF使用中讀取一個配置文件獲得一個結構體list,然後將數據綁定到Combobox下拉列表框中,如何實現

itemssource綁定後台的observablecollection<結構體,最好轉換成類,需要有屬性>
綁定text屬性到後台的selecteditem屬性。

② .NET 配置文件:為什麼這么做,存放在何處,如何使用求答案

我想如果我提供一個對這些文件的快速入門會對大家有些幫助。 在本文章中,許多 C# 源碼例子都假設你的項目已經引用了 System.Configuration.dll 和引用了下面的命名空間: using System.Configuration; 這是使用ConfigurationManager類所必須的,而這個類提供了一種使用配置信息的方法。 Why The .NET framework provides a rich set of classes, and techniques, to simplify application configuration. Essentially, all of these classes make it easy to read and write that configuration information from an XML configuration file. The configuration file includes a number of standard sections, some custom sections for common .NET features, and also allows the developer to create their own custom configuration sections. The standard sections have evolved over time. Initially, standard configuration was done mostly through theappSettingssection, which contains name / value pairs for each setting. Over time, transparent, type-safe support was provided via a generated C#Settingsclass and the sections. 譯者信息為什麼NET框架提供了一套豐富的類和技術,以簡化應用配置。從本質上講,所有這些類可以很容易地從XML配置文件的讀取和寫入,配置信息。配置文件包含了.net程序中的一些標準的以及自定義的節點,並且也允許開發者創建自己的配置節點。標准節點隨著時間跟以前比有了很大的改變。最開始的時候,標准節點主要是配置應用程序的配置內容,比如為一個屬性一個屬性或者一個值。隨著時間的推移,它也為類型安全提供了支持,同時可以生成C#標準的配置信息以及用戶自定義的配置信息
Where Where do I find the configuration file? This is a deceptively complicated problem. Since configuration is hierarchical, there are actually multiple configuration files that may affect an application. These include the machine configuration file, the application (or web) configuration file, the user local settings file, and the user roaming settings file. Machine Configuration The machine configuration file lives with the, not so easily found, .NET framework files. The location of the configuration file is dependent on the version of .NET and type of platform (e.g. 64 bit) used by your application. A typical example, might be: C:\Windows\Microsoft.NET\Framework\v4.0.30319\CONFIG\machine.config In your C# application code, the following will return the location of the file: System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() + @"CONFIG\machine.config"譯者信息
何處 我從哪裡找到配置文件?這是一個迷惑性的復雜問題。自從配置文件分層後,有多個配置文件可能影響一個應用程序。這包括機器的配置文件,應用程序(或者網頁)配置文件,用戶本地設置文件,用戶的Roaming設置文件。 機器配置 和.NET框架文件一起的機器配置文件,並不是很容易找到。配置文件的位置還取決於.NET的版本和應用程序使用的平台(比如,64位) 一個典型的例子就是
C:\Windows\Microsoft.NET\Framework\v4.0.30319\CONFIG\machine.config 在你的C#應用程序代碼中,下面的語句將會返迴文件的位置: System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() + @"CONFIG\machine.config" Application Configuration The application configuration file usually lives in the same directory as your application. For web applications, it is named Web.config. For non-web applications, it starts life with the name of App.config. Following a build, it is copied to the same name as your .exe file. So, for the program MyProgram.exe, you might expect to find MyProgram.exe.config, in the same directory. In your C# application code, the following will return the location of the file: AppDomain.CurrentDomain.SetupInformation.ConfigurationFile While it is not generally recommended, you may find that some applications alter the location of the application configuration file as follows: AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "NewName.config")譯者信息
應用配置文件
應用程序配置文件一般跟你的應用程序存在於相同的目錄下面。對於web應用程序來說,它的名字是Web.config,而對一般的應用程序來說,它的名字是App.config。在一個項目下,它的名字格式與你的.exe文件相似。比如你的工程名字是MyProgram.exe,那麼你就可以在相同的路徑下找到MyProgram.exe.config。 在你的C#應用程序源代碼中,使用下面的代碼可以返迴文件的路徑: AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 如果它不是被經常調用,你可以做在應用程序的配置文件中做一些小的修改。下面是例子: AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "NewName.config") User Settings The user settings are almost impossible to find, perhaps by design. The names of the directories vary with versions of Windows. To complicate matters further, the parent folder is generally hidden. The folder structure also incorporates the company name (of the application vendor), the application name, a unique identity for the application, and the application version. An example, on Windows 7, for local user settings might look like: C:\Users\MyUsername\AppData\Local\CompanyName\MyProgram.exe_Url_\1.0.0.0\user.config In C#, you can get the base directory for local user settings (first line) or roaming user settings (second line) as follows: Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) In C# (see notes in Overview), you can get the exact file path for local user settings (first line) or roaming user settings (second line) as follows: ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming).FilePath譯者信息
用戶設置 用戶設置大多數時候不好找,這很可能是處於設計的原因。目錄的名字因Windows版本而異。更復雜的是父目錄通常是隱藏的。. 目錄結構加入了公司名稱(應用程序的供應商),應用程序名稱,應用程序唯一ID號和應用程序的版本。 舉個例子,在Windows7下,一個本地用戶設置可能像這樣: C:\Users\MyUsername\AppData\Local\CompanyName\MyProgram.exe_Url_\1.0.0.0\user.config 在C#中,你可以獲得本地用戶設置的基目錄(第一行)或者臨時用戶設置(第二行): Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 在C#中, (見在概述中的注記),你可以獲得本地用戶設置的解壓文件路徑(第一行)或者roaming用戶設置(第二行): ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming).FilePath Other Configuration If this isn't all confusing enough, you should be aware of a few other files. There is a root Web.config (located in the same directory as machine.config). Also, sub-directories of a web application may provide additional overrides of inherited settings, via a Web.config specific to that sub-directory. Lastly, IIS provides some of its own configuration. A typical location would be: C:\Windows\System32\inetsrv\ApplicationHost.config How As mentioned earlier, the application configuration file is broken into a number of fairly standard configuration sections. Here, we briefly discuss a few of the most common sections. 譯者信息
其它配置 如果這還不夠混亂,那你應該知道其它的一些文件了(這個不會翻譯)。有個原始的Web.config文件(與machine.config同一個目錄下)。此外,子目錄下面的Web應用程序可能會通過子目錄裡面的Web.config重寫繼承(於父目錄的Web.config)的設置。 此外,IIS提供了一些自己的配置。一個典型的例子位置在: C:\Windows\System32\inetsrv\ApplicationHost.config 如何 正如前面提到的,應用程序配置文件被分解成若乾的相當標准配置部分。在這里,我們簡要地討論一下一些最常見的部分。 appSettings Section The simplest of the standard configuration sections isappSettings, which contains a collection of name / value pairs for each of the settings: <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="MySetting" value="MySettingValue" /> </appSettings> </configuration> In C# (see notes in Overview), you can reference the value of a setting as follows: string mySetting = ConfigurationManager.AppSettings["MySetting"]; connectionStrings Section Since database connections are so common in .NET, a special section is provided for database connection strings. The section is calledconnectionStrings: <?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="MyConnectionStringName" connectionString="Data Source=localhost;Initial Catalog=MyDatabase;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration> In C#, you can get the connection string as follows: string connectionString = ConfigurationManager.ConnectionStrings[ "MyConnectionStringName"].ConnectionString; Initially, one might wonder at the need to reference aConnectionStringproperty of a "connection string". In truth, the connectionStrings section is poorly named. A better name might have beenconnectionStringSettings, since each entry contains both a connection string and a database provider. The syntax of a connection string is wholly determined by the database provider. In this caseSystem.Data.SqlClient, is the most common database provider for the Microsoft SQL Server database. 譯者信息
appSettings 部分 最簡單的標准設置部分就是 appSettings 了,這個部分包含了一系列保存配置的 鍵/值 對。 <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="MySetting" value="MySettingValue" /> </appSettings> </configuration> 在C#中(見附註概述),你可以通過下面方式引用對應配置的值: string mySetting = ConfigurationManager.AppSettings["MySetting"]; connectionStrings 部分 由於資料庫連接在.NET中相當普遍,一個特別用於提供資料庫連接字元串的部分產生了。這個部分就是 connectionStrings。 <?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="MyConnectionStringName" connectionString="Data Source=localhost;Initial Catalog=MyDatabase;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration> 在C# 中,你可以通過下面方式去獲取連接字元串: string connectionString = ConfigurationManager.ConnectionStrings[ "MyConnectionStringName"].ConnectionString; 起初人們可能會奇怪在需要引用一個"connection string"屬性作為連接字元串。說實話,這個connectionStrings部分的名字真不恰當。叫做"connectionStringSettings"會更恰當,因為(部分_裡面的每個實體夠包含了連接字元串和database provider(資料庫提供者)。 一個連接字元串的語法完全取決於其database provider。 因此 System.Data.SqlClient 是Microsoft SQL Server最典型的database provider。 applicationSettings and userSettings Section With .NET 2.0, Microsoft tried to make it even easier to use configuration files. They introced a settings file. A careful observer will note that the "settings" start their life in the application configuration file and, later, get copied to the user settings configuration file. With Windows Form and WPF applications, you'll find a file Settings.settings in the Properties folder of your project. For Console applications, and others, you can also take advantage of settings. Open the Properties for your project, and click on the Settings button/tab. You'll be offered the option of adding a default settings file. Typically, you edit this settings file (or the settings for your project) rather than editing the configuration file directly. The example below is provided only to demonstrate that the settings do actually live in the configuration file. <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <section name="WinFormConfigTest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> </sectionGroup> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <section name="WinFormConfigTest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <userSettings> <WinFormConfigTest.Properties.Settings> <setting name="MyUserSetting" serializeAs="String"> <value>MyUserSettingValue</value> </setting> </WinFormConfigTest.Properties.Settings> </userSettings> <applicationSettings> <WinFormConfigTest.Properties.Settings> <setting name="MyApplicationSetting" serializeAs="String"> <value>MyApplicationSettingValue</value> </setting> </WinFormConfigTest.Properties.Settings> </applicationSettings> </configuration> To reference one of the settings, you simply use theSettingsclass, which is automatically created for you. A typical reference, might look as follows: string myUserSetting = Properties.Settings.Default.MyUserSetting; string myApplicationSetting = Properties.Settings.Default.MyApplicationSetting; Note:Propertiesis a namespace that is automatically created in your application's name space. To change a user's settings, you simply assign a value to the property and save the changes, as follows: Properties.Settings.Default.MyUserSetting = newValueForMyUserSetting; Properties.Settings.Default.Save();譯者信息
applicationSettings 和 userSettings 部分 在.NET 2.0 中,微軟嘗試讓用戶更容易使用設置文件。他們為此引入了設置文件。細心的觀察者可能會注意到這些"settings"開始用於應用程序配置文件,並且在後面復制到用於配置文件中。 在Windows Form和WPF程序中,你可以在你的項目的Properties目錄下找到一個名為Settings.settings的文件。對於控制台程序還有其它程序,可以通過下面方式使用配置文件。打開你的項目中屬性,切換到 設置 選項,你可以通過這里為項目添加一個配置文件。 通常情況下,你可以編輯此設置文件(或者是你的項目設置)來修改配置,而不是直接編輯(.config)配置文件。下面的例子演示了設置在配置文件中如何存儲。 <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <section name="WinFormConfigTest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> </sectionGroup> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <section name="WinFormConfigTest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <userSettings> <WinFormConfigTest.Properties.Settings> <setting name="MyUserSetting" serializeAs="String"> <value>MyUserSettingValue</value> </setting> </WinFormConfigTest.Properties.Settings> </userSettings> <applicationSettings> <WinFormConfigTest.Properties.Settings> <setting name="MyApplicationSetting" serializeAs="String"> <value>MyApplicationSettingValue</value> </setting> </WinFormConfigTest.Properties.Settings> </applicationSettings> </configuration> 當你想引用設置的時候,你可以簡單的引用Settings類,這個類會自動為你創建。下面是一個典型的引用方式: string myUserSetting = Properties.Settings.Default.MyUserSetting; string myApplicationSetting = Properties.Settings.Default.MyApplicationSetting; 注意:Properties 命名空間會自動的創建在你的應用程序的命名空間下。 要改變用戶設置時,你只需像下面一樣為屬性賦予一個值然後保存就可以了: Properties.Settings.Default.MyUserSetting = newValueForMyUserSetting; Properties.Settings.Default.Save(); Upgrading Settings Eventually, you'll want to release a new version of your application. Here, you may encounter a common problem. Since the user settings are version specific, they will be lost following the upgrade. Thankfully, the framework anticipates this requirement and provides theUpgrademethod. A typical way of handling this is to include a booleanUpgradeser setting, with an initial value of false (when your application is first deployed). So, typical code to handle the upgrade (and retain previous user settings) looks as follows: if (!Properties.Settings.Default.Upgraded) { Properties.Settings.Default.Upgrade(); Properties.Settings.Default.Upgraded = true; Properties.Settings.Default.Save(); }譯者信息
更新設置 實際上,當你想發布一個新版本的程序時,你可能會遇到的一個普遍問題。由於用戶設置是有特定版本的,程序升級會導致這些設置丟失。 值得慶幸的是,框架預預料到這種情況並提供了一個更新設置的方法。一個典型的處理辦法是引入一個初始值為false的用戶設置「Upgraded」(當你首次部署你的程序)。 因此,用來處理升級的典型代碼(並保留以前的用戶設置)如下所示: if (!Properties.Settings.Default.Upgraded) { Properties.Settings.Default.Upgrade(); Properties.Settings.Default.Upgraded = true; Properties.Settings.Default.Save(); } What Next In the previous section, you may have noticed the rather verboseconfigSectionssection of the configuration file. This is how Microsoft extended the configuration file to add the new userSettings and applicationSettings sections. It is also how you can add your own custom configuration sections. In a nutshell, you do this by extending . Within each of your derived classes, you will be decorating the members with attributes . Simple right? Just kidding. Others have provided excellent descriptions of this slightly complicated, but not too difficult mechanism. I include links at the end of this document for further reading. Here, I only wanted to provide a couple of hints that can get lost in the longer descriptions. 譯者信息
下一步做什麼 在前一部分,你可能注意到了配置文件相當冗長的configSections。這也是微軟如何拓展配置文件,用來添加新的用戶設置和應用設置。 它也是你如何來添加你自己定製的配置的區塊。 簡而言之,你通過拓展 類來完成的。在你的每一個繼承的類里,你會用類似這樣的屬性來布置你的類成員。 很簡單,對嗎?開個玩笑。對於其他的,已經提供了很好很詳細,稍微有點復雜的描述,和不是很難理解的機制。我會在這篇文檔的末尾添加這些鏈接供進一步閱讀。 這里,我只想提供一些在冗長描述中會感到困惑的提示。

③ wpf 我改了settings裡面屬性的值 為什麼讀出來的還是以前的

你的Settings的scope是User吧?User級別的會在每個用戶的目錄下保存配置文件(如果你保存過的話),Properties.Settings.Default是在沒有用戶級別配置文件存在的情況下,讀取當前目錄App.config中的配置文件

所以你需要改為Application級別,然後自己寫Save方法(Application級別Settings的Save方法是無效的)

④ 在WPF程序運行時動態修改app.config文件後如何立即生效

你在config.Save() 後加這句試試:
ConfigurationManager.RefreshSection("appSettings");

⑤ c#- wpf架構讀取xml配置文件到界面列表內,無思路求幫助

不用mvvm的話,so easy
新建 一個silverlight 4工程 在Window1.xaml加入一個comboBox1
後台中加個student 的類
public class Student
{
public string Name{get;set;}
public int Age{get;set;}
}

public Window1()
{
InitializeComponent();
List<Student> StudList =new List<Student>();
StudList.Add(new Student(){ Name="張飛",Age=21 });
StudList.Add(new Student(){ Name="曹操",Age=22 });
StudList.Add(new Student(){ Name="關公",Age=23 });
StudList.Add(new Student(){ Name="如花",Age=24 });
comboBox1.ItemsSource=StudList;
comboBox1.DisplayMemberPath="Name";
}

運行下看看
你List 想讀配置喊頃文件的話Student 這個類喚滲襲就跟著變下,wpf基於屬性綁定,不是事件驅動,想入門的話建議看下[深入淺出WPF完整版]劉鐵猛出的,目前國人出的wpf就他的寫的不錯的書
還有你的配置文件應該是xml格式文件,要新建個MeterList的類 要用 System.Xml.XmlDocument這和兄個類來讀配置文件,來填沖MeterList這個類

⑥ wpf連結資料庫的問題

不管是WPF還是WinForm還是ASP.NET,這些都只是表現層的展現方式不同,而到了後台,所有的都是一樣的,都有一個配置文件可以進行配置並讀取的。如果你的項目中沒有這個文件,那就自己添加一個app.config即可。即添加文件中的應用程序配置文件

⑦ wpf把集合寫進配置文件,讀取的時候每個元素顯示為行

stringss=ConfigurationManager.AppSettings["name"];
侍搜foreach(stringsinss.Split(newchar[]{渣談歷','},
StringSplitOptions.RemoveEmptyEntries))
如搜this.lb.Items.Add(s);

⑧ wpf如何讀取一個config配置文件格式的xml文件

<Title Row="0" Cloumn="2" ContentTitle="aaa" Assemblydll="aaa.dll" InterFace="aaaaaa" ImageUrl="pack://application:,,,/TyPlatform;component/Images/111.png" Width="180" Height="180" ></Title>
就像這樣的一個XML,讀成DataSet,然後這樣取值
DataTable dt;
DataSet ds = new DataSet();
ds.ReadXml(Environment.CurrentDirectory + "/" + path);
dt = ds.Tables[0];

string Row = dt.Rows[index]["Row"].ToString();
string Cloumn = dt.Rows[index]["Cloumn"].ToString();
string Height = dt.Rows[index]["Height"].ToString();
string Width = dt.Rows[index]["Width"].ToString();
string ImageUrl = dt.Rows[index]["ImageUrl"].ToString();

⑨ 如何給wpf程序啟動需要參數如何設置

右鍵點擊你的項目抄--選擇保持默認名稱,否則你的程序讀取App.config時得到的值是空的。

2
打開App.config,添加應用程序設置的5個鍵值

first_run: 應用程序第一次正常啟動
server: 伺服器IP
database: 資料庫名稱
uid: 資料庫用戶名
pwd: 資料庫密碼

資料庫伺服器配置在程序首次啟動時的值都是空的

3
右鍵點擊項目中的應用,添加一個引用,然後再你需要訪問配置文件的類頂部添加 using

閱讀全文

與wpf讀配置文件相關的資料

熱點內容
微信恢復聊天記錄iphone 瀏覽:155
索浪磁碟無文件 瀏覽:910
哪些材料做成紅頭文件 瀏覽:212
maya做玻璃瓶教程 瀏覽:586
一條小米數據線大概多少錢 瀏覽:443
如何匹配兩個excel表格相同的數據 瀏覽:120
蘋果筆記本文件怎麼找回 瀏覽:426
資料庫怎麼用游標 瀏覽:722
不同頻率的數據如何共享 瀏覽:83
什麼時候說解讀文件 瀏覽:165
揚州前端程序員私活網站有哪些 瀏覽:657
怎麼改手機網路好一點 瀏覽:702
淺談微信卡券功能開發 瀏覽:511
線切割如何用電腦編程 瀏覽:227
vba如何獲取已經打開的excel文件 瀏覽:209
什麼是投標文件名 瀏覽:390
電腦網路斷開了怎麼辦 瀏覽:492
數控車床直角怎麼清角怎麼編程 瀏覽:414
如何從流量競爭到數據智能化 瀏覽:176
不想升級優酷 瀏覽:305

友情鏈接