Location>code7788 >text

Applications get the current directory and exit

Popularity:907 ℃/2025-01-26 14:28:27

As the title C#get ​​the directory and exit of the current running application

Preface

In today's impetuous society, leaders are always patting themselves on the back, everyone is trying to make ends meet, and they are under pressure to make progress. In this environment, everyone has no choice but to treat work with a brick-and-mortar attitude, using search engines to find and then copy it everywhere. The code is right. I looked for a method, and there is one that returns the root directory of the current application ().Location returns the path of the currently executed assembly. I even know other ways. Don't say, hey. . . . . I copied a piece of code and tried it, and it still works and achieves the effect.

1 //Through Assembly2 //Get the full path of exe file
3 string appPath2 = ().Location;
4 //Get the directory where the exe file is located
5 string appPath1 = ;

Process approach

1 //Can get the main window title
2  var mainwindowTitle = ().MainWindowTitle;
3 //Note that you cannot get the exe file path in this way.
4 var appPath5 = ().;
5 //This method can obtain the full path of EXE file in the current process
6 var appPath6 = ().;

 

Concept combing

If you have a few concepts, please know clearly. The pile of EXE we run by itself is called Assembly, which is also called processmbly, also known as processes. Also pay attention to the above code to obtain the EXE execution program path

1 string appPath2 = ().Location;
2 var appPath6 = ().;

The original concept is the path to the file of the "main running module", because the code may not be run directly from you and may be loaded from other places.

  • Assembly emphasizes the internal function and method metadata definition of the call or DLL that is called in sense.
  • Process is essentially the current IF ELSE judgment. In the sense, the application process scheduled by the operating system and the memory area controlled by the operation system.

We all call it "things", application objects, we know that there is Application in VC ++.

1 int CMFCh11App::ExitInstance()
2 {
3     //TODO: Handle additional resources that may have been added
4     AfxOleTerm(FALSE);
5 
6     return CWinApp::ExitInstance();
7 }

These things call themselves "application", so we can find applications with Winform in Winform.

1 //Get the full path of exe file
2 string appPath3 = ;
3 //Get the directory where the exe file is located
4 string appPath4 = ;

You can find a wpf application in wpf. Note that the wpf application cannot obtain the directory where the exe is running.

1 string appstr = ();

The concept of the essence of regression

The essence of this thing should be natural. The operating system should give application-level information, and the application should naturally know where it is started. The essence of things, um, good, returns to the essence of things. When we perform file interface operations, isn’t it Directory? When we write files, isn’t it the ./" directory? That means writing to the current application directory. Shouldn’t this thing be natural? Yes.

1 DirectoryInfo dir = new DirectoryInfo("./");
2 //Output d:\xxx\
3 ();
4 //Output d:\xxx
5 (());

Pay attention to this ending. An extra slash in () will cause trouble.

About exiting the application

How does the application exit? Think about why we are called programmers, aren’t we just writing assembly line jobs? Not that lofty. Computer operation requires a continuous flow of instructions. Think about programming in the DOS era or on a single-chip microcomputer. When there are no instructions to feed, it will naturally end. Any special operations you need are not required at all.

1 class Program
2 {
3     static void Main(string[] args)
4     {
5         ("aaa");
6         //The application is over here
7     }
8 }

Okay, you have to get out of the horns. You have to exit halfway. If you do not have to return, you can do it, or you can

1 (0);

Therefore, the DOS program does not have the concept of "I don't know when I will exit" at all. Only the graphical interface "does not know when I will quit" because the window itself is maintained through the Windows message mechanism through the Windows message mechanism in a dead cycle cycle.

winform exit

1 ();

wpfexit

1 (0);

Since only winform was supported at the beginning and later wpf was added, it can be seen from the namespace that there are many overlaps in the actual functions and concepts of the entire .NetFramework.