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 ?
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.
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.