tags:

views:

120

answers:

4

I am writing the necessary business logic in C# methods by using class library in .net. In each class of class library I am writing single method which servers the specific purpose. My project folder containing the class library is stored on F: drive. I have added one folder named XML to class library & added the XML file in that. Now how should I give the application path in C# & then use that path for giving path to the XML file ? Can you please provide me the code or any link through which I can resolve the above issue ? Is there any other way (different from above way) in which we can give the path of the XML file dynamically ?

A: 

Are you looking for Application.ExecutablePath? Then you can use IO.Path.Combine(IO.Path.GetDirectoryName(Application.ExecutablePath),"XML") to get the location of the xml file you need.

hometoast
Fair answer, but unfortunately WinForms-specific — cannot be used with a console app or with WPF...
Timwi
Ah, right. I do live in a winforms world here.
hometoast
You can try to use the first string returned by Environment.GetCommandLineArgs instead of Application.Executable path.
munissor
@munissor: From the docs: *The program file name can, but is not required to, include path information.* Unfortunately, you can't rely on `Environment.GetCommandLineArgs` for path information because of this.
OregonGhost
I know, I just commented to "try" if it works, maybe for Shailesh's case it's acceptable..
munissor
+2  A: 

Do you have the XML files relative to your executable / class library? If so, just get the assembly object and retrieve its location.

Use System.Reflection.Assembly.GetExecutingAssembly() to get an assembly object. This will be the assembly that calls GetExecutingAssembly - so if you're calling this in the class library, it will return the class library assembly. Assembly.Location contains the path to the assembly, and you can use the functions in System.IO.Path to modify the path to point to a subdirectory.


If, as you indicated in a comment, the XML files are embedded resources, you can use code like the following to retrieve them:

var asm = System.Reflection.Assembly.GetExecutingAssembly();
foreach (string resourceName in asm.GetManifestResourceNames()) {
    var stream = asm.GetManifestResourceStream(resourceName);
    // Do something with stream
}

In this case, I don't know if there's any possibility to use the path of one of the files to load it, but most .NET classes dealing with files can use a stream anyway.

OregonGhost
+2  A: 

AppDomain.CurrentDomain.BaseDirectory

Scordo
I don't understand how this can be the accepted answer. Neither is this necessarily the path of the class library (just tested: in this test, it's the executable's directory), nor can it be used to load embedded resources, which is what Shailesh Jaiswal requested in the comment to a (now deleted) answer. I'm not downvoting since this may be what he actually wanted to ask for, though then it's totally unclear from the question.
OregonGhost
Scordo
@Scordo: No offense, it's ok. I just don't understand why this was accepted, since Shailesh Jaiswal said he wanted to extract embedded resources, and/or wanted to have a path relative to a class library, but he accepted an answer that will give him a path relative to the executable (likely) and won't extract resources. It just doesn't match exactly with the additional information. However. Doesn't matter.
OregonGhost
Yepp, I know what you mean
Scordo
A: 

I believe this question will solve your problem.

antur123