C# 设置 xml 文件位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/650100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Settings xml file location
提问by Tom J Nowell
I have a project that reads in a file based on a value in a C# Setting class. This value however changes from machine to machine and Id rather not ask the user the first time the program is run as its a corporate environment, instead Id like it to be set in the installer, but where is the file located? Is there an easier method?
我有一个项目,它根据 C# Setting 类中的值读入文件。然而,这个值会随着机器的不同而变化,并且在程序第一次作为公司环境运行时,我不会询问用户,而是喜欢在安装程序中设置它,但是文件在哪里?有没有更简单的方法?
This is a visual studio addin not a standalone program
这是一个视觉工作室插件,而不是一个独立的程序
采纳答案by abmv
From your post it appears you have a windows application? , you can store an initial value in the application config, you can make an installer in Visual Studio and write custom actions that can write values to the file on first install in you install project.
从您的帖子看来,您有一个 Windows 应用程序?,您可以在应用程序配置中存储初始值,您可以在 Visual Studio 中创建安装程序并编写自定义操作,这些操作可以在安装项目中首次安装时将值写入文件。
回答by Aamir
Registry or an INI file or a XML file whatever suits you best.
最适合您的注册表或 INI 文件或 XML 文件。
回答by Shoban
Best option would be registry. Istalling the application will require Admin access so writing to the registry during installation will not be an issue.
最好的选择是注册表。安装应用程序将需要管理员访问权限,因此在安装期间写入注册表将不是问题。
More over if some one accidently deletes the ini or settings file then the program might stop working.
更重要的是,如果有人不小心删除了 ini 或设置文件,那么程序可能会停止工作。
回答by Groo
I believe you are talking about designer-generated settings (the .settings file)?
我相信您在谈论设计器生成的设置(.settings 文件)?
The exact path usually contains some sort of a Hash (check thislink). I usually have my own settings class which I serialize to and from xml using XmlSerializer, which gives me more freedom (I think C# settings files don't allow you to add custom enums, for example, or they make it a bit harder to do it than simply adding them to the .settings file).
确切的路径通常包含某种哈希(检查此链接)。我通常有我自己的设置类,我使用XmlSerializer将其序列化到 xml 和从 xml 序列化,这给了我更多的自由(例如,我认为 C# 设置文件不允许您添加自定义枚举,或者它们使它变得有点难做它不仅仅是将它们添加到 .settings 文件中)。
However, maybe there is no need to set values during installation? For example, you could add a FirstStartup
setting (set to true
initially), which you can read when your App is started for the first time, and then set it to false
. That way you can set your default settings when you detect a "first startup".
但是,也许在安装过程中不需要设置值?例如,您可以添加一个FirstStartup
设置(true
初始设置为),您可以在您的应用程序第一次启动时读取该设置,然后将其设置为false
。这样您就可以在检测到“首次启动”时设置默认设置。
回答by Peter Tate
You will certainly need some sort of custom action at the end of your installer. You haven't mentioned what technology you're using to deploy your application so I will refrain from giving any specific guidance.
在安装程序结束时,您肯定需要某种自定义操作。您还没有提到您使用什么技术来部署您的应用程序,因此我不会提供任何具体的指导。
I recommend setting the value in your App.config
. This is an xml file which will be named MyApplication.exe.config
in the same directory as your application. Add it to your installer if it is not there already. In your installer, add a new setting:
我建议在您的App.config
. 这是一个 xml 文件,将MyApplication.exe.config
在与您的应用程序相同的目录中命名。如果它不存在,请将其添加到您的安装程序中。在您的安装程序中,添加一个新设置:
<configuration>
<appSettings>
<add key="MySetting" value="My Value"/>
</appSettings>
</configuration>
In your code, retrieve the setting:
在您的代码中,检索设置:
String setting = ConfigurationSettings.AppSettings["MySetting"];
If this is a machine-wide setting, this installer is the right place to set this. If you do it on the first execution you will run into a host of problems with permissions on the files and directories if the first person to run the app is a user with limited permissions.
如果这是一个机器范围的设置,这个安装程序是设置它的正确位置。如果您在第一次执行时执行此操作,如果第一个运行该应用程序的用户是权限有限的用户,您将遇到一系列文件和目录权限问题。
回答by Peter Tate
My case is different, my project is a class library (dll) which have the app.config file. I decided to create 4 application settings variables (title, url, limit, rate). To create this, i right-click ont he project --> Properties --> Settings. this values you can retrieve in code using this command --> Settings.Default.title, ...etc
我的情况不同,我的项目是一个类库(dll),其中包含 app.config 文件。我决定创建 4 个应用程序设置变量(标题、网址、限制、速率)。要创建它,我右键单击项目--> 属性--> 设置。您可以使用此命令在代码中检索此值 --> Settings.Default.title, ...etc
the problem is let say i instantiate 5 objects (same object for example MyProject.MyClass) from this library, i want the instance be able to have its own settings. i mean each of the instance may have their xml setting file. can this be done?
问题是我从这个库中实例化了 5 个对象(相同的对象,例如 MyProject.MyClass),我希望实例能够有自己的设置。我的意思是每个实例都可能有它们的 xml 设置文件。可以这样做吗?
回答by Sesh
The configure file is the easiest way to do what you are asking, however it is NOT the best. In fact it is recommended Not to use .config files for such cases.
配置文件是执行您要求的最简单方法,但它不是最好的方法。事实上,建议不要在这种情况下使用 .config 文件。
whenever users install an 'update', there is a risk of overwriting their existing changes.
some businesses might have policy restrictions on the .config files.
the user cannot easily move his settings from one PC to another.
每当用户安装“更新”时,就有覆盖他们现有更改的风险。
一些企业可能对 .config 文件有政策限制。
用户无法轻松地将其设置从一台 PC 移动到另一台 PC。
From my own experience, using XML, MS-Access, Registry or text files to store user settings has proven more useful than using the .config files.
根据我自己的经验,使用 XML、MS-Access、注册表或文本文件来存储用户设置已被证明比使用 .config 文件更有用。
回答by Ryan Bolger
You said that this is for a corporate environment. You can create an administrative template for group policy to set the default in the registry. Then, your program can just query that one registry value if the default hasn't already been set.
你说这是针对企业环境的。您可以为组策略创建管理模板以在注册表中设置默认值。然后,如果尚未设置默认值,您的程序就可以查询该注册表值。
回答by gatapia
I personally would never use web.config or app.config. Just read your own xml file, have a look at my post on this: http://www.picnet.com.au/blogs/Guido/post/2009/09/10/XML-Settings-Files-No-more-webconfig.aspx
我个人永远不会使用 web.config 或 app.config。只需阅读您自己的 xml 文件,看看我的帖子:http: //www.picnet.com.au/blogs/Guido/post/2009/09/10/XML-Settings-Files-No-more-网页配置文件
Thanks
谢谢
Guido
圭多
回答by Noob-o-tron 5000
To answer the "Where is the file located" bit of the original question for those interested (I'm assuming that the "settings file" in question is the "Settings File" item available from the "Add New Item" dialogue).
为那些感兴趣的人回答原始问题的“文件位于何处”位(我假设有问题的“设置文件”是“添加新项目”对话框中可用的“设置文件”项目)。
In Win7 (and probably all the other ones) the settings file will generate under the path:
在 Win7(可能还有所有其他的)中,设置文件将在路径下生成:
C:\Users\[user]\AppData\Local
C:\用户\[用户]\AppData\Local
The folder structure from here on out depends on your AssemblyInfo and build information, the "AssemblyCompany" field (if populated) is used for the top-most folder, beneath this is a folder copying the application executable name, followed by the build and then finally the settings file (named "*.config").
从这里开始的文件夹结构取决于您的 AssemblyInfo 和构建信息,“AssemblyCompany”字段(如果已填充)用于最顶层的文件夹,在此下方是复制应用程序可执行文件名称的文件夹,然后是构建,然后最后是设置文件(名为“*.config”)。
In my case, the complete path is as follows:
就我而言,完整路径如下:
C:\Users\[user]\AppData\Local\[company_name]\Application.exe_StrongName_zx0v1qxyoass4d0z3mtwe3vbrhaehkjg\0.0.5812.21662\user.config
C:\Users\[用户]\AppData\Local\[company_name]\Application.exe_StrongName_zx0v1qxyoass4d0z3mtwe3vbrhaehkjg\0.0.5812.21662\user.config