Console app problem

M

^MisterJingo^

Hi all,

I've got a very small app which I want to read an xml config file which
is stored in the directory of the exe. To do this I am using the
following line of code:

XmlTextReader reader = new
XmlTextReader(System.IO.Directory.GetCurrentDirectory() +
"\\SDRApp.xml");

If I open a cmd window and type the path of the .exe file, I get an
error saying file cannot be found, it seems it is trying to look for
the file in the currently browsed folder of the cmd window (e.g. c:\).
If I traverse to the correct directory before executing the app, it all
works perfectly.
Should I be using another command other than
System.IO.Directory.GetCurrentDirectory() to get the directory the
application is executing from? This seems to look at where I am calling
the .exe from, rather than where the .exe is executing itself.

Thanks for any help.
 
P

pranesh.nayak

Just try using:
Application.ExecutablePath;
OR
Application.StartupPath;


Also you can use:
System.Reflection.Assembly.GetExecutingAssembly.Location
 
M

Marcin Hoppe

^MisterJingo^ said:
This seems to look at where I am calling
the .exe from, rather than where the .exe is executing itself.

Yes, that is how it works. To get the location of the *.exe file you
might for instance want to use the location of the "root" assembly that
started the whole call stack (possibly crossing many assemblies):

using System.IO;
using System.Reflection;
using System.Xml;

namespace App
{
class Program
{
static void Main()
{
string assemblyPath = Assembly.
GetEntryAssembly().
Location;

string path = Path.Combine( assemblyPath,
"SDRApp.xml");

XmlReader reader = XmlReader.Create(path);
// ...
}
}
}

Best regards!
Marcin
 

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