开发者

WPF C# XML file added as resource

开发者 https://www.devze.com 2023-02-25 23:06 出处:网络
I\'m using an XML file added into my project with following properties : Build Action : Resource Copy to output directory : Copy Always

I'm using an XML file added into my project with following properties :

Build Action : Resource
Copy to output directory : Copy Always

Then when the executable is running, I need the users to be able to edit the XML (add, remove and modify nodes). Is it possible ? Or do I need 开发者_如何学编程to change the Build Action to Content ?

Thanks

edit : i've removed it and added again as Content. But now I have another issue : When I add it to the project, it looks for it in the app root directory (WpfApplication1\WpfApplication1\myfile.xml). But then when I run the app, the function in charge to add a node works fine, but it saves the file to the WpfApplication1\WpfApplication1\bin\Debug\ directory ! Hence the "real" myfile.xml is not updated .

I'm using :

XMLHosts.Load("myfile.xml");
XMLHosts.Save("myfile.xml");

and the datasource is declared as :

<XmlDataProvider x:Key="MyfileData"  Source="myfile.xml" XPath="Books/Book" />


If you want the XML to be loaded from disc (and modified), use Build Action = None and Copy = Copy Always.

An issues you will run into is that if you install your app to "Program Files", you will not be able to edit the file on Windows Vista/7 unless you running as admin. - "Program Files" is only editable during install.

A good practice would be to copy XML to %APPDATA%\Your app (APPDATA = environmental variable, access using System.Environment). You can preform this copy either during install, or during first run. %APPDATA% is meant for runtime changeable settings.

If you mark your XML as None+CopyAlways, you can copy it from execution directory to %APPDATA%\Your app on application startup, if it does not yet exist at destination address (if not yet copied).

You can then load it from %APPDATA%\Your app and read the latest settings.

0

精彩评论

暂无评论...
验证码 换一张
取 消