Deploying apps with XML files

G

Guest

I have written a short program which reads from XML and flat text files. I
have deployed the apllication wwith the files included but the app can no
longer find the files.


In the code, I read from the XML file with:

Dataset1.ReadXML("..\\..\\Student.xml");

This ran fine during debugging as the ..\\..\\ simply bumps it up a couple
of folders to the bin folder, where it can be read. However, once deployed
it bumped the search up to my C:\ drive. I tried to create a sub-folder
under the application folder in my set up but it still couldn't find the file.

Can anyone please tell me how to deploy an application which reads from text
or XML files.
 
J

Jochen Kalmbach

Hi =?Utf-8?B?Y2FzaGRlc2ttYWM=?=,
In the code, I read from the XML file with:

Dataset1.ReadXML("..\\..\\Student.xml");

This ran fine during debugging as the ..\\..\\ simply bumps it up a
couple of folders to the bin folder, where it can be read. However,
once deployed it bumped the search up to my C:\ drive. I tried to
create a sub-folder under the application folder in my set up but it
still couldn't find the file.

You are reading from an relative path!
If the file is shipped with your app you should consider using the Path
of your exe as start-path...


// Assuming Student.xml is on the same dir as the exe:
Dataset1.ReadXML(System.IO.Path.Combine(App.StartupPath, "Student.xml"));



See: How to get the .NET applications path?
http://blog.kalmbachnet.de/?postid=7


<code>
<public class App
{
[System.Runtime.InteropServices.DllImport("kernel32.dll",
SetLastError=true)]
[System.Security.SuppressUnmanagedCodeSecurity]
private static extern uint GetModuleFileName(
[System.Runtime.InteropServices.In]
IntPtr hModule,
[System.Runtime.InteropServices.Out]
System.Text.StringBuilder lpFilename,
[System.Runtime.InteropServices.In]
[System.Runtime.InteropServices.MarshalAs(
System.Runtime.InteropServices.UnmanagedType.U4)]
int nSize
);

public static string StartupPath
{
get
{
System.Text.StringBuilder sb = new System.Text.StringBuilder
(260);
if (GetModuleFileName(System.IntPtr.Zero, sb, sb.Capacity) == 0)
throw new System.ApplicationException("Could not retrive module
file name");
return System.IO.Path.GetDirectoryName(sb.ToString());
}
}
}
</code>


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top