文件处理程序的配置文件|配置文件ini有什么作用

㈠ .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。这也是微软如何拓展配置文件,用来添加新的用户设置和应用设置。 它也是你如何来添加你自己定制的配置的区块。 简而言之,你通过拓展 类来完成的。在你的每一个继承的类里,你会用类似这样的属性来布置你的类成员。 很简单,对吗?开个玩笑。对于其他的,已经提供了很好很详细,稍微有点复杂的描述,和不是很难理解的机制。我会在这篇文档的末尾添加这些链接供进一步阅读。 这里,我只想提供一些在冗长描述中会感到困惑的提示。

㈡ 配置文件ini有什么作用

新建记事本文件,然后改后缀为ini,里面的东西当然是自己写了。 搂主真是太天才了。 给你个程序的例子吧,手上没别的程序的配置文件,就拿放了很久的黑守给你说吧。 主程序名为hxdef.exe,附带一个启动批处理文件,用记事本打开批处理文件里面写的是: hxdef.exe hxdef.ini,意思是启动hxdef.exe和hxdef.ini,hxdef.ini则为 hxdef.exe的配置文件。当然其他的程序不会有这个批处理,他们调用ini文件是在自身程序里就设置好了的,ini文件的名字是固定的,不可改变,如果要改变就必须改变这个批处理或者说程序里面的设定。好了来看下这个ini里面的内容: [Hidden Table] hxdef* [Root Processes] hxdef* [Hidden Services] hxdef* …… 这就是参数,这些参数是这个程序所特有的,也就是说其他程序即使有这些参数功能也不见得一样。 这些参数的意思是, [Hidden Table] 是需要隐藏的文件、目录和进程的列表。 这将隐藏以“hxdef”开头的所有文件、目录和进程。下面的依此类推。 [Hidden Services] 隐藏的服务和驱动文件列表。 [Hidden RegKeys] 隐藏的注册表键名列表。 当然这些参数的意思都是在说明文件里面公开了的,很多程序是不会公开自己ini配置文件的参数的,如果你想知道你就去网上找,看看有没人知道,或者你自己慢慢试,这都是试出来的。 你也会看到某些 *****=****的内容,这个前面的那段是参数名称,等号后面是参数值。 怎么说呢,它就相当于一个静态的程序控制器,如果程序必须要有ini文件才能运行的话。 ini文件中都保存着程序的基本配置信息,程序在启动的时候调用里面的数据,根据里面的数据调整自身设置并启动。 不同程序的ini文件是不同的,所以没有通用性可言。不过有些喜欢偷懒的程序员编的程序也说不定。 下面是windows系统文件夹的简单配置信息: 无论资源管理器使用哪种风格,这些设置都将有效。可以使用下面的步骤来用Desktop.ini自定义一个文件夹的风格: 1. 使用PathMakeSystemFolder()函数使一个文件夹变成系统文件夹,当然也可以在命令行键入"attrib +s 文件夹名"来把文件夹变成系统文件夹。 2. 按照上面说明的格式和自己的要求,在文件夹里建立一个Desktop.ini文件。 以下是一个用Desktop.ini定制文件夹的例子: 使用缺省的模板 [.ShellClassInfo] ConfirmFileOp=1 删除、移动时提示 NoSharing=1 不能共享 IconFile=Folder.ico 自己的图标 IconIndex=0 第一个图标 InfoTip=测试Desktop.ini 提示信息

㈢ 配置文件有什么用

用户配置文件 问:什么是用户配置文件? 答:用户配置文件就是在用户登录时定义系统加载所需环境的设置和文件的集合。它包括所有用户专用的配置设置,如程序项目、屏幕颜色、网络连接、打印机连接、鼠标设置及窗口的大小和位置。 当你第一次登录到一台基于Micr–s–ft Wind–ws XP、Wind–ws 2000或Wind–ws NT? W–rkstati–n的计算机上时,系统就会为你创建一个专用的配置文件。 问:配置文件都有什么不同的区别? 答:有三种主要的配置文件类型: ·本地用户配置文件。在用户第一次登录到计算机上时被创建,这个本地用户配置文件被储存在计算机的本地硬盘驱动器上。任何对本地用户配置文件所作的更改都只对发生改变的计算机产生作用。 ·漫游用户配置文件。一个本地配置文件的副本被复制及储存在网络上的一个服务器共享上。当用户每次登录到网络上的任一台计算机上时,这个文件都会被下载,并且当用户注销时,任何对漫游用户配置文件的更改都会与服务器的拷贝同步。漫游用户配置文件要求这台计算机是Wind–ws NT域或Active Direct–ry?的一个成员。 ·强制用户配置文件。是一种特殊类型的配置文件,使用它管理员可为用户指定特殊的设置。只有系统管理员才能对强制用户配置文件作修改。当用户从系统注销时,用户对桌面做出的修改就会丢失。 第四种类型的配置文件是一个临时的配置文件,只有在因一个错误而导致用户配置文件不能被加载时才会出现。临时配置文件允许用户登录并改正任何可能导致配置文件加载失败的配置。临时配置文件在每次会话结束后都将被删除–注销时对桌面设置和文件所作的更改都会丢失。 问:我查看了"D–cuments and Settings"文件夹,发现有许多配置文件。它们都是做什么用的? 答:Wind–ws为每个登录到计算机上的用户都创建了一个配置文件。除这些配置文件外,还有一些"特殊"的配置文件: –默认用户。默认用户配置文件被用作任何新用户的起始点。当一个用户第一次登录到计算机时,Wind–ws将创建一个新文件夹,用来储存新用户的配置文件,并且将默认的配置文件复制到这个新文件夹中。用户对默认配置文件所作的更改都被记录到用户的拷贝中。缺省情况下,默认用户配置文件的属性是隐藏的。 –所有用户。每个用户的"开始"菜单和桌面包含所有项目,这些内容是从"所有用户"的配置文件以及他或她自己的配置文件中来的。从"所有用户"的配置文件中取得的项目被作为公用程序项,系统上的每个用户都能看到这些。如果你想要保证每个登录的用户都能访问一个程序或文件,那么就将它的快捷方式放进"所有用户"的配置文件中即可,但是一定要小心,如果一个用户删除了此快捷方式或文件,那么对所有用户来说,它都会被删除。 –网络服务和本地服务。网络服务和本地服务的配置文件是由Wind–ws XP为两个新的内置账号自动创建的,它们被服务控制管理器用来管理本地系统账号不需要运行的服务。这些配置文件需要由系统来运行,并且不应当被修改。默认情况下,所有这些配置文件都是隐藏的。 问:我不喜欢这些配置文件,能不能像在Wind–ws 95/98/Me中那样,只要有一个配置文件就能用于所有用户? 答:不能实现。用户配置文件是Wind–ws 2000和Wind–ws XP的一个基本部分。如果你不希望每个用户都有他或她自己单独的配置文件,只需简单地让每个用户以相同的用户账号进行登录即可。这将给你与Wind–ws 95/98/Me相类似的体验。 问:我安装了一个应用程序,并且我能看到这个程序的快捷方式,但其他用户却看不到,为什么? 答:这是因为,此程序只为你的配置文件安装,而不是为所有用户。在Wind–ws XP和Wind–ws 2000中,每个用户都有他或她自己个人的开始菜单。一些应用程序将在安装时提示你决定是否为所有用户安装,或者是仅为当前用户安装。 你可以从你的Start Menu中将快捷方式简单地复制到All Users Pr–file的Start Menu(通常是C:/D–cuments and Settings/All Users/Start Menu)中。 问:如何查看我的配置文件的内容? 答:每个使用Wind–ws资源管理器的人,可从"我的电脑"进入到C:/D–cuments and Settings/,找到你的用户名,或者单击"开始"按钮,选择"运行",敲入%USERPR–FILE%,然后按 回车键。就会打开一个显示你的配置文件内容的窗口。 问:我重新安装了Wind–ws,现在我的所有设置和文件都不见了–我怎样才能找回它们? 答:如果你在现有的安装上重新安装了Wind–ws,则配置文件可能还保留着。使用Wind–ws资源管理器,进入到C:/D–cuments and Settings/,然后查找与你的用户名相匹配的文件夹。你就可以从这个文件夹中复制任何文件到新的配置文件中。 问:在哪能找到关于用户配置文件的更多信息? 答:请在Wind–ws XP站点上查阅白皮书:在Wind–ws 2000环境中管理Wind–ws XP的用户数据和设置。 漫游配置文件 问:我如何配置一个漫游用户配置文件? 答:你可以使用下面的步骤配置漫游配置文件。 若要为用户配置一个漫游配置文件: 1、在将要储存用户配置文件的服务器上创建一个文件夹。这将是一个顶级的文件夹,其中包含了所有单独的用户配置文件。 2、将这个文件夹配置成为一个共享的文件夹,并且授予所有用户"完全控制"的权限。 3、打开Active Direct–ry用户和计算机控制台插件,然后找到目标用户。 4、右键单击用户名,然后再弹出的快捷菜单上单击"属性"。 5、单击"配置文件"选项卡。 6、在配置文件路径栏中,输入用户配置文件将要被储存的网络共享的路径。例如,对于一个网络名是Jd–e的用户来说,输入下面的路径"//Netw–rkShare/Pr–files/%username%",配置文件将创建一个叫做Jd–e的目录,该目录在服务器上被共享并且用于储存用户配置文件。 关于配置漫游用户配置文件的更多信息,清查阅用户数据和用户设置的循序渐进指南。 问:我正在使用漫游配置文件,可是登录速度很慢–怎样才能让它更快一些? 答:登录的速度与配置文件的大小及网络的速度有直接的关系。你可以做许多事情来限制配置文件的大小: ·重定向大型的文件夹,如"My D–cuments",这将会减少登录和注销时将数据复制到计算机或从计算机复制数据的数量。 ·将大的文件从漫游配置文件中排除出来。默认情况下,大型的文件夹,如"L–cal Settings"、"Temp"和"Temp–rary Internet Files"不会被漫游。你可以使用"在漫游配置文件中排除目录"的组策略设置来添加一个新文件夹到排除列表;一旦这些文件夹被包含在策略中,在登录时它们就将被复制到本地计算机上,并且注销时不会复制回服务器。 ·如果你通过一个慢速连接登录到服务器,则设置合适的"慢速网络"超时设置。 ·不要在用户配置文件中保存大文件。 问:如何处理关于用户配置文件的问题? 答:处理的第一步应该是在客户计算机上检查应用程序事件日志,然后检查是否存在错误。如果这是一个漫游配置文件,请检查是否为其设置了正确的权限(具体信息可以在用户数据和设置白皮书中找到)–导致漫游用户配置文件出错的最常见的一个原因就是在配置文件上设置了错误的权限。除了在应用程序事件日志中记录事件外,用户配置文件还提供了一个详细的日志来帮助处理问题。如果希望为用户配置文件创建一个详细的日志文件,您可以: –启动注册表编辑器,定位下面的路径: HKEY_L–CAL_MACHINE/S–ftware/Micr–s–ft/Wind–wsNT/CurrentVersi–n/Winl–g–n –创建一个叫做UserEnvDebugLevel的新值,将其标记为REG_DW–RD,然后将它的值以16进制格式设置为30002。 –这个日志文件就是%windir%/debug/userm–de/userenv.l–g

㈣ 如何做一个配置文件

新建文本文档。[.ShellClassInfo]IconFile=%SystemRoot%\system32\SHELL32.dllIconIndex=4ExtShellFolderViews] {BE098140-A513-11D0-A3A4-00C04FD706EC}={BE098140-A513-11D0-A3A4-00C04FD706EC} [{BE098140-A513-11D0-A3A4-00C04FD706EC}] Attributes=1 IconArea_Image=h:\deskpic\1.jpg IconArea_Text=0x000000c0 [.ShellClassInfo] ConfirmFileOp=0 [Ctrl+A Select All] 命名desktop,ini放到你想要的文件回夹答下。

㈤ java程序打包EXE后,配置文件如何处理

写一段读写配置文件的代码,通过你的程序去改,岂不是很方便?

㈥ 配置文件格式用哪个

App.config文件1. 配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的。它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序。配置文件的根节点是configuration。我们经常访问的是appSettings,它是由.Net预定义配置节。我们经常使用的配置文件的架构是象下面的形式。先大概有个印象,通过后面的实例会有一个比较清楚的认识。下面的“配置节”可以理解为进行配置一个XML的节点。 常见配置文件模式: <configuration> <configSections> //配置节声明区域,包含配置节和命名空间声明 <section> //配置节声明<sectionGroup> //定义配置节组<section> //配置节组中的配置节声明 <appSettings> //预定义配置节 <Custom element for configuration section> //配置节设置区域 2. 只有appSettings节的配置文件及访问方法 下面是一个最常见的应用程序配置文件的例子,只有appSettings节。 程序代码: [ 复制代码到剪贴板 ] <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="connectionstring" value="User ID=sa;Data Source=.;Password=;Initial Catalog=test;Provider=SQLOLEDB.1;" /> <add key="TemplatePATH" value="Template" /> </appSettings> </configuration> 下面来看看这样的配置文件如何方法。 程序代码: [ 复制代码到剪贴板 ] string _connectionString=ConfigurationSettings.AppSettings["connectionstring"]; 使用ConfigurationSettings类的静态属性AppSettings就可以直接方法配置文件中的配置信息。这个属性的类型是NameValueCollection。 3. 自定义配置文件 3.1自定义配置节 一个用户自定义的配置节,在配置文件中分为两部分:一是在<configSections></ configSections> 配置节中声明配置节(上面配置文件模式中的“<section>”),另外是在<configSections>< / configSections >之后设置配置节(上面配置文件模式中的“< Custom element for configuration section>”),有点类似一个变量先声明,后使用一样。声明一个配置文件的语句如下: <section name=" " type=" "/> <section>:声明新配置节,即可创建新配置节。 name:自定义配置节的名称。 type:自定义配置节的类型,主要包括System.Configuration.SingleTagSectionHandler、 System.Configuration.DictionarySectionHandler、 System.Configuration.NameValueSectionHandler。 不同的type不但设置配置节的方式不一样,最后访问配置文件的操作上也有差异。下面我们就举一个配置文件的例子,让它包含这三个不同的type。 程序代码:<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="Test1" type="System.Configuration.SingleTagSectionHandler"/> <section name="Test2" type="System.Configuration.DictionarySectionHandler"/> <section name="Test3" type="System.Configuration.NameValueSectionHandler" /> </configSections> <Test1 setting1="Hello" setting2="World"/> <Test2> <add key="Hello" value="World" /> </Test2> <Test3> <add key="Hello" value="World" /> </Test3> </configuration> 我们对上面的自定义配置节进行说明。在声明部分使用<section name="Test1" type= "System.Configuration.SingleTagSectionHandler"/>声明了一个配置节它的名字叫Test1,类型为SingleTagSectionHandler。在设置配置节部分使用 <Test1 setting1= "Hello" setting2="World"/>设置了一个配置节,它的第一个设置的值是Hello,第二个值是World,当然还可以有更多。其它的两个配置节和这个类似。 下面我们看在程序中如何访问这些自定义的配置节。我们用过ConfigurationSettings类的静态方法GetConfig来获取自定义配置节的信息。 程序代码: public static object GetConfig(string sectionName); 下面是访问这三个配置节的代码: 程序代码: //访问配置节Test1 IDictionary IDTest1 = (IDictionary)ConfigurationSettings.GetConfig("Test1"); string str = (string)IDTest1["setting1"] +" "+(string)IDTest1["setting2"]; MessageBox.Show(str); //输出Hello World //访问配置节Test1的方法2 string[] values1=new string[IDTest1.Count]; IDTest1.Values.CopyTo(values1,0); MessageBox.Show(values1[0]+" "+values1[1]); //输出Hello World //访问配置节Test2 IDictionary IDTest2 = (IDictionary)ConfigurationSettings.GetConfig("Test2"); string[] keys=new string[IDTest2.Keys.Count]; string[] values=new string[IDTest2.Keys.Count]; IDTest2.Keys.CopyTo(keys,0); IDTest2.Values.CopyTo(values,0); MessageBox.Show(keys[0]+" "+values[0]); //访问配置节Test3 NameValueCollection nc=(NameValueCollection)ConfigurationSettings.GetConfig("Test3"); MessageBox.Show(nc.AllKeys[0].ToString()+" "+nc["Hello"]); //输出Hello World 通过上面的代码我们可以看出,不同的type通过GetConfig返回的类型不同,具体获得配置内容的方式也不一样。 [table] 配置节处理程序|返回类型[br] [/table] SingleTagSectionHandler Systems.Collections.IDictionary DictionarySectionHandler Systems.Collections.IDictionary NameValueSectionHandler Systems.Collections.Specialized.NameValueCollection 3.2自定义配置节组 配置节组是使用<sectionGroup>元素,将类似的配置节分到同一个组中。配置节组声明部分将创建配置节的包含元素,在< configSections>元素中声明配置节组,并将属于该组的节置于<sectionGroup>元素中。下面是一个包含配置节组的配置文件的例子: 复制代码 代码如下:<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="TestGroup"> <section name="Test" type="System.Configuration.NameValueSectionHandler"/> </sectionGroup> </configSections> <TestGroup> <Test> <add key="Hello" value="World"/> </Test> </TestGroup> </configuration>

㈦ app.config 配置文件属于什么格式

App.config文件1. 配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的。它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序。配置文件的根节点是configuration。我们经常访问的是appSettings,它是由.Net预定义配置节。我们经常使用的配置文件的架构是象下面的形式。先大概有个印象,通过后面的实例会有一个比较清楚的认识。下面的“配置节”可以理解为进行配置一个XML的节点。 常见配置文件模式: <configuration> <configSections> //配置节声明区域,包含配置节和命名空间声明 <section> //配置节声明 <sectionGroup> //定义配置节组 <section> //配置节组中的配置节声明 <appSettings> //预定义配置节 <Custom element for configuration section> //配置节设置区域 2. 只有appSettings节的配置文件及访问方法 下面是一个最常见的应用程序配置文件的例子,只有appSettings节。 程序代码: [ 复制代码到剪贴板 ] <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="connectionstring" value="User ID=sa;Data Source=.;Password=;Initial Catalog=test;Provider=SQLOLEDB.1;" /> <add key="TemplatePATH" value="Template" /> </appSettings> </configuration> 下面来看看这样的配置文件如何方法。 程序代码: [ 复制代码到剪贴板 ] string _connectionString=ConfigurationSettings.AppSettings["connectionstring"]; 使用ConfigurationSettings类的静态属性AppSettings就可以直接方法配置文件中的配置信息。这个属性的类型是NameValueCollection。 3. 自定义配置文件 3.1自定义配置节 一个用户自定义的配置节,在配置文件中分为两部分:一是在<configSections></ configSections> 配置节中声明配置节(上面配置文件模式中的“<section>”),另外是在<configSections>< / configSections >之后设置配置节(上面配置文件模式中的“< Custom element for configuration section>”),有点类似一个变量先声明,后使用一样。声明一个配置文件的语句如下: <section name=" " type=" "/> <section>:声明新配置节,即可创建新配置节。 name:自定义配置节的名称。 type:自定义配置节的类型,主要包括System.Configuration.SingleTagSectionHandler、 System.Configuration.DictionarySectionHandler、 System.Configuration.NameValueSectionHandler。 不同的type不但设置配置节的方式不一样,最后访问配置文件的操作上也有差异。下面我们就举一个配置文件的例子,让它包含这三个不同的type。 程序代码:<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="Test1" type="System.Configuration.SingleTagSectionHandler"/> <section name="Test2" type="System.Configuration.DictionarySectionHandler"/> <section name="Test3" type="System.Configuration.NameValueSectionHandler" /> </configSections> <Test1 setting1="Hello" setting2="World"/> <Test2> <add key="Hello" value="World" /> </Test2> <Test3> <add key="Hello" value="World" /> </Test3> </configuration> 我们对上面的自定义配置节进行说明。在声明部分使用<section name="Test1" type= "System.Configuration.SingleTagSectionHandler"/>声明了一个配置节它的名字叫Test1,类型为SingleTagSectionHandler。在设置配置节部分使用 <Test1 setting1= "Hello" setting2="World"/>设置了一个配置节,它的第一个设置的值是Hello,第二个值是World,当然还可以有更多。其它的两个配置节和这个类似。 下面我们看在程序中如何访问这些自定义的配置节。我们用过ConfigurationSettings类的静态方法GetConfig来获取自定义配置节的信息。 程序代码: public static object GetConfig(string sectionName); 下面是访问这三个配置节的代码: 程序代码: //访问配置节Test1 IDictionary IDTest1 = (IDictionary)ConfigurationSettings.GetConfig("Test1"); string str = (string)IDTest1["setting1"] +" "+(string)IDTest1["setting2"]; MessageBox.Show(str); //输出Hello World //访问配置节Test1的方法2 string[] values1=new string[IDTest1.Count]; IDTest1.Values.CopyTo(values1,0); MessageBox.Show(values1[0]+" "+values1[1]); //输出Hello World //访问配置节Test2 IDictionary IDTest2 = (IDictionary)ConfigurationSettings.GetConfig("Test2"); string[] keys=new string[IDTest2.Keys.Count]; string[] values=new string[IDTest2.Keys.Count]; IDTest2.Keys.CopyTo(keys,0); IDTest2.Values.CopyTo(values,0); MessageBox.Show(keys[0]+" "+values[0]); //访问配置节Test3 NameValueCollection nc=(NameValueCollection)ConfigurationSettings.GetConfig("Test3"); MessageBox.Show(nc.AllKeys[0].ToString()+" "+nc["Hello"]); //输出Hello World 通过上面的代码我们可以看出,不同的type通过GetConfig返回的类型不同,具体获得配置内容的方式也不一样。 [table] 配置节处理程序|返回类型[br] [/table] SingleTagSectionHandler Systems.Collections.IDictionary DictionarySectionHandler Systems.Collections.IDictionary NameValueSectionHandler Systems.Collections.Specialized.NameValueCollection 3.2自定义配置节组 配置节组是使用<sectionGroup>元素,将类似的配置节分到同一个组中。配置节组声明部分将创建配置节的包含元素,在< configSections>元素中声明配置节组,并将属于该组的节置于<sectionGroup>元素中。下面是一个包含配置节组的配置文件的例子: 复制代码 代码如下:<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="TestGroup"> <section name="Test" type="System.Configuration.NameValueSectionHandler"/> </sectionGroup> </configSections> <TestGroup> <Test> <add key="Hello" value="World"/> </Test> </TestGroup> </configuration>

㈧ 如何指定.NET程序运行时需要读取的配置文件路径

WCF和EF都需要配置文件,而且默认的名字都是app.config,在程序中,无需我们处理配置文件(比如读XML文件,然后获取节点信息),在WCF的Host端和EF里面只需如下的两段代码,.NET能自动获取app.config的内容:// WCF工程using (var host=new ServiceHost(typeof(Server.Settings.Configration))) { host.Open(); Console.WriteLine("WCF服务已经启动,按任意键终止……"); Console.Read(); } // EF工程using (var context=new SettingsEntities()) { } 但是!如果把app.config重命名为other.config(或其它名字,但内容不变),程序将无法正常运行,它会告诉你找不到配置文件。 请问如何使用任意名称的配置文件,同时代码简洁,最好不要直接操作XML文件。我知道.NET中有个类叫 System.Configuration.ConfigurationManager,但不知道如何用在WCF或EF的配置读取中。而且ConfigurationManager也只是方便的读取XML而已。我希望ServiceHost或ObjectContext有这样一个属性:string filePath = "otherName.config";****.ConfigurationFilePath = filePath;当然,如果能指定程序运行时要读取的配置文件,更好。

㈨ 配置文件是什么意思

配置文件即对不同对象进行不同配置的文件。 配置文件FAQ 用户配置文件 问:什么是用户配置文件? 答: 当你第一次登录到一台基于Micr–s–ft Wind–ws XP、Wind–ws 2000或Wind–ws NT? W–rkstati–n的计算机上时,系统就会为你创建一个专用的配置文件。 来保存用户的屏幕颜色、网络连接、打印机连接、鼠标设置及窗口的大小和位置。 等 等 问:配置文件都有什么不同的区别? 答:有三种主要的配置文件类型: ·本地用户配置文件。在用户第一次登录到计算机上时被创建,这个本地用户配置文件被储存在计算机的本地硬盘驱动器上。任何对本地用户配置文件所作的更改都只对发生改变的计算机产生作用。 ·漫游用户配置文件。一个本地配置文件的副本被复制及储存在网络上的一个服务器共享上。当用户每次登录到网络上的任一台计算机上时,这个文件都会被下载,并且当用户注销时,任何对漫游用户配置文件的更改都会与服务器的拷贝同步。漫游用户配置文件要求这台计算机是Wind–ws NT域或Active Direct–ry?的一个成员。 ·强制用户配置文件。是一种特殊类型的配置文件,使用它管理员可为用户指定特殊的设置。只有系统管理员才能对强制用户配置文件作修改。当用户从系统注销时,用户对桌面做出的修改就会丢失。 第四种类型的配置文件是一个临时的配置文件,只有在因一个错误而导致用户配置文件不能被加载时才会出现。临时配置文件允许用户登录并改正任何可能导致配置文件加载失败的配置。临时配置文件在每次会话结束后都将被删除–注销时对桌面设置和文件所作的更改都会丢失。 问:我查看了"D–cuments and Settings"文件夹,发现有许多配置文件。它们都是做什么用的? 答:Wind–ws为每个登录到计算机上的用户都创建了一个配置文件。除这些配置文件外,还有一些"特殊"的配置文件: –默认用户。默认用户配置文件被用作任何新用户的起始点。当一个用户第一次登录到计算机时,Wind–ws将创建一个新文件夹,用来储存新用户的配置文件,并且将默认的配置文件复制到这个新文件夹中。用户对默认配置文件所作的更改都被记录到用户的拷贝中。缺省情况下,默认用户配置文件的属性是隐藏的。 –所有用户。每个用户的"开始"菜单和桌面包含所有项目,这些内容是从"所有用户"的配置文件以及他或她自己的配置文件中来的。从"所有用户"的配置文件中取得的项目被作为公用程序项,系统上的每个用户都能看到这些。如果你想要保证每个登录的用户都能访问一个程序或文件,那么就将它的快捷方式放进"所有用户"的配置文件中即可,但是一定要小心,如果一个用户删除了此快捷方式或文件,那么对所有用户来说,它都会被删除。 –网络服务和本地服务。网络服务和本地服务的配置文件是由Wind–ws XP为两个新的内置账号自动创建的,它们被服务控制管理器用来管理本地系统账号不需要运行的服务。这些配置文件需要由系统来运行,并且不应当被修改。默认情况下,所有这些配置文件都是隐藏的。 问:我不喜欢这些配置文件,能不能像在Wind–ws 95/98/Me中那样,只要有一个配置文件就能用于所有用户? 答:不能实现。用户配置文件是Wind–ws 2000和Wind–ws XP的一个基本部分。如果你不希望每个用户都有他或她自己单独的配置文件,只需简单地让每个用户以相同的用户账号进行登录即可。这将给你与Wind–ws 95/98/Me相类似的体验。 问:我安装了一个应用程序,并且我能看到这个程序的快捷方式,但其他用户却看不到,为什么? 答:这是因为,此程序只为你的配置文件安装,而不是为所有用户。在Wind–ws XP和Wind–ws 2000中,每个用户都有他或她自己个人的开始菜单。一些应用程序将在安装时提示你决定是否为所有用户安装,或者是仅为当前用户安装。 你可以从你的Start Menu中将快捷方式简单地复制到All Users Pr–file的Start Menu(通常是C:/D–cuments and Settings/All Users/Start Menu)中。 问:如何查看我的配置文件的内容? 答:每个使用Wind–ws资源管理器的人,可从"我的电脑"进入到C:/D–cuments and Settings/,找到你的用户名,或者单击"开始"按钮,选择"运行",敲入%USERPR–FILE%,然后按 回车键。就会打开一个显示你的配置文件内容的窗口。 问:我重新安装了Wind–ws,现在我的所有设置和文件都不见了–我怎样才能找回它们? 答:如果你在现有的安装上重新安装了Wind–ws,则配置文件可能还保留着。使用Wind–ws资源管理器,进入到C:/D–cuments and Settings/,然后查找与你的用户名相匹配的文件夹。你就可以从这个文件夹中复制任何文件到新的配置文件中。 问:在哪能找到关于用户配置文件的更多信息? 答:请在Wind–ws XP站点上查阅白皮书:在Wind–ws 2000环境中管理Wind–ws XP的用户数据和设置。 漫游配置文件 问:我如何配置一个漫游用户配置文件? 答:你可以使用下面的步骤配置漫游配置文件。 若要为用户配置一个漫游配置文件: 1、在将要储存用户配置文件的服务器上创建一个文件夹。这将是一个顶级的文件夹,其中包含了所有单独的用户配置文件。 2、将这个文件夹配置成为一个共享的文件夹,并且授予所有用户"完全控制"的权限。 3、打开Active Direct–ry用户和计算机控制台插件,然后找到目标用户。 4、右键单击用户名,然后再弹出的快捷菜单上单击"属性"。 5、单击"配置文件"选项卡。 6、在配置文件路径栏中,输入用户配置文件将要被储存的网络共享的路径。例如,对于一个网络名是Jd–e的用户来说,输入下面的路径"//Netw–rkShare/Pr–files/%username%",配置文件将创建一个叫做Jd–e的目录,该目录在服务器上被共享并且用于储存用户配置文件。 关于配置漫游用户配置文件的更多信息,清查阅用户数据和用户设置的循序渐进指南。 问:我正在使用漫游配置文件,可是登录速度很慢–怎样才能让它更快一些? 答:登录的速度与配置文件的大小及网络的速度有直接的关系。你可以做许多事情来限制配置文件的大小: ·重定向大型的文件夹,如"My D–cuments",这将会减少登录和注销时将数据复制到计算机或从计算机复制数据的数量。 ·将大的文件从漫游配置文件中排除出来。默认情况下,大型的文件夹,如"L–cal Settings"、"Temp"和"Temp–rary Internet Files"不会被漫游。你可以使用"在漫游配置文件中排除目录"的组策略设置来添加一个新文件夹到排除列表;一旦这些文件夹被包含在策略中,在登录时它们就将被复制到本地计算机上,并且注销时不会复制回服务器。 ·如果你通过一个慢速连接登录到服务器,则设置合适的"慢速网络"超时设置。 ·不要在用户配置文件中保存大文件。 问:如何处理关于用户配置文件的问题? 答:处理的第一步应该是在客户计算机上检查应用程序事件日志,然后检查是否存在错误。如果这是一个漫游配置文件,请检查是否为其设置了正确的权限(具体信息可以在用户数据和设置白皮书中找到)–导致漫游用户配置文件出错的最常见的一个原因就是在配置文件上设置了错误的权限。除了在应用程序事件日志中记录事件外,用户配置文件还提供了一个详细的日志来帮助处理问题。如果希望为用户配置文件创建一个详细的日志文件,您可以: –启动注册表编辑器,定位下面的路径: HKEY_L–CAL_MACHINE/S–ftware/Micr–s–ft/Wind–wsNT/CurrentVersi–n/Winl–g–n –创建一个叫做UserEnvDebugLevel的新值,将其标记为REG_DW–RD,然后将它的值以16进制格式设置为30002。 –这个日志文件就是%windir%/debug/userm–de/userenv.l–g

㈩ 如何修改配置文件

1. 引言OpenWRT中采用LuCI作为它的Web interface界面框架,采用Lua语言。在本文中将以一个简单的示例详细描述如何自定义开发一个界面,对一个配置文件进行操作。2.Model与ControlerMVC的设计理念是进行LuCI开发的一个关键在LuCI中Controller的文件定义在固件中的/usr/lib/lua/luci/controller目录中,模版目录在/usr/lib/lua/luci/view目录下,而model则是在/usr/lib/lua/luci/model中。而model中有一个特殊的模块叫做CBI,被称为LuCI中最酷的功能,该模块的功能是方便的对一个配置文件进行修改。3.示例本文中的页面建立在LuCI界面的network下,不单独创建页面,因此无需写view,只用些controller和model就可以了。1)首先创建一个controllerccontroller/mycbi.luamole("LUCI.controller.mycbi", package.seeall)function index()entry({"admin", "network", "mycbi_change"}, cbi("mycbi-model/mycbimole"), "Change My Conf", 30).dependent=falseend解释一下关键代码:在index()函数中,使用entry函数来完成每个模块函数的注册,官方说明文档如下:entry(path, target, title=nil, order=nil)path is a table that describes the position in the dispatching tree: For example a path of {"foo", "bar", "baz"} would insert your node in foo.bar.baz.target describes the action that will be taken when a user requests the node. There are several predefined ones of which the 3 most important (call, template, cbi) are described later on on this pagetitle defines the title that will be visible to the user in the menu (optional)order is a number with which nodes on the same level will be sorted in the menu (optional)其中target主要分为三类:call,template和cbi。call用来调用函数,template用来调用已有的htm模版,而CBI模块则是使用非常频繁也非常方便的模块,包含的一系列lua文件构成界面元素的组合,所有cbi模块中的控件都需要写在luci.cbi.Map中,在cbi模块中定义各种控件,Luci系统会自动执行大部分处理工作。在cbi.lua文件中封装了所有的控件元素,例如复选框,下拉列表等。2)创建model#mkdir /usr/lib/lua/luci/model/cbi/mycbi-model#vim /usr/lib/lua/luci/model/cbi/mycbi-model/mycbimole.luam = Map("mycbi", "mycbi conf change interface")s = m:section(TypedSection, "MySection")s.addremove = trues:option(Value, "username", "Name:")key=s:option(Value, "password", "Password")key.password=true;return m解释一下关键代码:3)创建配置文件#vim /etc/config/mycbiconfig 'MySection' 'mycbi'option 'username' 'youruser'option 'password' 'yourpass'4. 测试进入OpenWRT界面,登陆后就可以点击“网络”,如果是英文就点击network,可以看到我们添加的子页面入口:点击后进入页面如下:输入用户名密码:root/test,点击保存,后台查看配置文件已经被更改:5. 问题记录1)首先,配置文件不能有任何后缀,否则页面加载后是空页面2)如果出现500 错误,说明lua文件写的有问题,要么是路径错误,要么是语法错误,暂时没找到写日志的方法,可以用wireshark抓包看错误


赞 (0)