简体   繁体   中英

Use external xml file after publishing the c# desktop application

I have developed ac# desktop application that needs to be hosted on a server and will be scheduled to fetch data based on queries stored in XML files. while developing, I was using the following code to read XML files:

var query = new XPathDocument(@"C:\\\\Documents and Settings\\\\XYZ\\\\Desktop\\\\productplanningquery.xml");

as you can see I had put the XML file conveniently on my desktop and it worked fine while development. What I want to do now, is to give at a path such that where ever I host the application plus the XML files, it does not throw an exception. One way i thought could be to have a folder in a directory where the application will be installed but for that i will have to figure out the path to current directory dynamically (which i could not figure out).

Please help.

You can use something like this:

var path = string.Format("{0}\\{1}", Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "productplanningquery.xml");
var pathForCurrentApp = string.Format("{0}\\{1}", Environment.CurrentDirectory, "productplanningquery.xml");

I am not sure what you want to achieve whether you want to read xml using winform app and do some operation and then pass it web app or some thing else. But here is my understandings:

Case 1: If you need to create XML outside the IIS and that XML will be consumed by ASP.Net app, then :

For using a desktop application with IIS server , you need to have full administrative access to the Live Machine. If its not then you should consider building windows services to operate on the XML files or any task that will run behind the scenes to decrease the load of the asp.net app.

Still in this case if you dont own a server then you need some Virtual Private Hosting or similar kind of hosting where you have almost all previleges to access the system. Then deploy the Windows Service, set the output path in such a manner so that it can be accessed by asp.net app too. And do whatever you want.

Case 2: If you want o read XML in ASP.Net Solely, then

In this case you case read it easily by using XDocument.But note, XML should in the same application directory or under the reach of the ASP.Net app

MSDN Article for Web-Windows Services with ASP.Net

You could pass the location of you XML using args this way you're code would look like so:

var query = new XPathDocument(args[0])

You can also use relative path. Make sure that when deploying your code you keep the location of the file in the same relative location. For example if you place the XML in the same directory as the application

var query = new XPathDocument("productplanningquery.xml")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM