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 ?
-
http://stackoverflow.com/questions/1762160/c-get-path-of-class-library – Arslan Aug 19 '10 at 14:39
4 Answers
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.

- 23,359
- 7
- 71
- 108
AppDomain.CurrentDomain.BaseDirectory

- 1,041
- 7
- 12
-
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 Aug 20 '10 at 08:12
-
I just concentrated on this sentence: >Now how should I give the application path in C# & then use that path for giving path to the XML file ? – Scordo Aug 20 '10 at 08:59
-
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 Aug 20 '10 at 12:42
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.

- 11,522
- 5
- 41
- 58
-
3Fair answer, but unfortunately WinForms-specific — cannot be used with a console app or with WPF... – Timwi Aug 19 '10 at 14:15
-
You can try to use the first string returned by Environment.GetCommandLineArgs instead of Application.Executable path. – munissor Aug 19 '10 at 14:20
-
@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 Aug 19 '10 at 14:25
-
I know, I just commented to "try" if it works, maybe for Shailesh's case it's acceptable.. – munissor Aug 19 '10 at 14:39