1

I'm using Windows CE 6.0. I'm developping an application that needs to read a XML file (bar.xml) that is placed right next to my executable (foo.exe).

I try to access it with the following method, called right after my Main() :

private void ParseXmlFile(string _sFileName)
{
    XmlDocument l_doc = new XmlDocument();
    l_doc.Load(_sFileName);
}

Now, when launching my application from the Windows CE console with :

foo.exe bar.xml

All I receive is an exception stating : Cound not find file '\bar. Notice the '\' here. I also tried :

foo.exe bar.xml
foo.exe .\bar.xml
foo.exe ./bar.xml

My application is under \Hard Disk\ftp\Test\

If I put my file under the "Hard Disk" folder, everything is fine. Of course, I don't want my file here. How can I tell my application to look up this file in the same folder as my application ?

Edit :
After comment from @Thomas, I checked my path and saw that I was indeed in the correct folder (\Hard Disk\ftp\Test).

I had to use the following code to get the path (because of Compact framework 2.0) :

string l_sFullAppName = Assembly.GetCallingAssembly().GetName().CodeBase;
string l_sFullAppPath = Path.GetDirectoryName(l_sFullAppName);

XmlDocument l_doc = new XmlDocument();
l_doc.Load(_l_sFullAppPath + '\\bar.xml');

It works but doesn't seem to be very convenient to me. Any other ideas ?

Andy M
  • 5,945
  • 7
  • 51
  • 96
  • Check with [Application.executablepath](https://msdn.microsoft.com/en-us/library/system.windows.forms.application.executablepath.aspx) if your application is really executed where you think it is. – Thomas Ayoub Jun 10 '15 at 08:53
  • It's a console application, I cannot have Application.executablepath. I tried following [this](http://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-a-net-console-application) but I can't get the Location property. I don't know why. My System.dll reference is under "C:\Program Files (x86)\Microsoft.NET\SDK\CompactFramework\v2.0\WindowsCE\System.dll" and is indeed in version 2.0. – Andy M Jun 10 '15 at 09:15
  • @leppie I specified c#-2.0 because I absolutely need framework 2.0. Is that wrong ? – Andy M Jun 10 '15 at 09:18
  • And this :`Environment.GetCommandLineArgs()[0];` ? – Thomas Ayoub Jun 10 '15 at 09:20
  • I don't have it, I guess it's because I'm using the framework 2.0 of compact framework. – Andy M Jun 10 '15 at 09:46

1 Answers1

2
  1. Determine full executable directory. See HOW TO: Determine the Executing Application's Path. The contents is applicable to .NET Compact Framework. Note, that System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase returns location of the assembly as a URL, but System.Reflection.Assembly.GetExecutingAssembly().Location returns full path or UNC location (see here).
  2. Use Path.Combine() method for combining strings into a result path.

As result your code may be like this:

var fullPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
var fullFileName = System.IO.Path.Combine(fullPath, _sFileName);
Didgeridoo
  • 1,222
  • 2
  • 14
  • 21
  • Thank you for your answer. I didn't know about Path.Combine(), I simply used string concatenation to add my filename to my filepath. That is interesting. Thank you for your time. – Andy M Jun 10 '15 at 11:50
  • No problem. Usage of `Path.Combine()` is the best practice to combine a path strings. For example see answer to [related question](http://stackoverflow.com/questions/10704579/best-practice-for-building-file-paths). – Didgeridoo Jun 10 '15 at 12:30