How to specify application path in C # .NET?

I am writing the necessary business logic in C # methods using the class library in .net. In each class of the class library, I write one method that serves a specific purpose. My project folder containing the class library is stored on drive F :. I added one folder named XML to the class library and added an XML file to it. Now, how should I specify the application path in C # and then use this path to specify the path to the XML file? Can you provide me with a code or any link through which I can solve the above problem? Is there any other way (different from the above) in which we can dynamically specify the path of the XML file?

+3
source share
4 answers

AppDomain.CurrentDomain.BaseDirectory

+2
source

Do you have any XML files regarding your binaries / classes library? If so, just get the assembly object and retrieve its location.

Use System.Reflection.Assembly.GetExecutingAssembly()to get the assembly object. This will be the assembly that calls GetExecutingAssembly - so if you call it in the class library, it will return the assembly of the class library. Assembly.Locationcontains the path to the assembly, and you can use the functions in System.IO.Pathto change the path to a pointer to a subdirectory.


If, as you noted in the comment, XML files are embedded resources, you can use the following code to get them:

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

, , .NET, , .

+2

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 that you need.

0
source

I believe this question will solve your problem.

0
source

Source: https://habr.com/ru/post/1760549/


All Articles