views:

228

answers:

3

My files are referenced like so (it's all relative):

// WHERE YOU KEEP THE PAGE TITLE XML
    public static string     myPageTitleXML = "xml/pagetitles.xml";

and

    using (StreamReader r = new StreamReader(myPageTitleXML))
    { //etc.. . .etc....etc..
    }

I get system.io.directorynotfound, and "this problem needs to be shut down", when I double click the executable. But running it from the console works like a charm. What's wrong here?

I played around with attempting to set Environment.CurrentDirectory but couldn't get anything to work. Why should I have to do that anyway? It defeats the purpose of a relative path no?

responding.. .

"application" does not exist in the current context, i'll keep trying what people have mentioned, this is not a windows.form

testing

Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), myPageTitleXML); gives error URI formats are not supported, as does Path.GetFullPath(). Server.MapPath results in an error as well, this is currently offline

A: 

Well assuming this directory is somewhere under the directory in which your code is executing, it sounds like you can use ..

Application.ExecutablePath()

or

Application.StartUpPath()

.. to get an idea as to what your application is seeing when it goes in search of an 'xml' directory with the 'pagetitles.xml' file in it.

If the directory returned by one of these methods does not point where you thought it did, you'll need to move the location of your application or the location of this folder so that it is within the same directory as the app.

Hope this gets you on the right path.

CLR
A: 

So, when you run it from double clicking the executable, is there a file named pagetitles.xml in a folder named xml, where xml is a folder in the same location as the executable?

It's certainly possible to use relative paths like this, but I wouldn't really recommend it. Instead, maybe use something like:

 string fileToOpen = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), myPageTitleXML);  
 using (StreamReader r = new StreamReader(fileToOpen))  
 {   
   //etc.. . .etc....etc..  
 }
marcc
A: 

Is this ASP.NET code? If so then you probably need to do MapPath("xml/pagetitles.xml")

Jason