Get the path where the program is located in .NET

xiaoxiao2021-03-06  68

In VB6, you can use the program

APP object

The Path property is to determine the path to the execution program. This is useful for loading a configuration file in a certain program path or establishing a temporary file.

However, App objects cannot be used in VB.NET. In VB.NET, an executable path can be obtained by two methods.

If you build a Windows form program, you can use the Application object. This object has many provider information and properties of provider information, including the ExecutablePath property and the StartUppath property.

The ExecutablePath property returns the full path and file name of the execution program, and the StartupPath property returns only the execution program is the path.

For a executor called Test.exe at C: / Temp Directory, its operational code is:

Debug.writeline (Application.executablePath) Debug.writeline (Application.Startuppath)

The procedural results are as follows:

C: /TEMP/Test.exec: / Temp

If the program is not based on a Windows form (such as console programs, class libraries, etc.), you can use a System.Reflection namespace. The Assembly class provides a location property that returns the program path and the file name.

In order to get the path of the program through the Assembly class, you can use the following code:

Debug.writeLine (_ system.reflection.assembly.getexecutingassembly.location)

For a program called Test.dll in C: / Temp, the above code returns:

C: /Temp/test.dll

Note that the Application.executablePath property and the assembly.location property return are the path and file name of the execution file.

转载请注明原文地址:https://www.9cbs.com/read-91875.html

New Post(0)