Openfiledialog dialog = new openfiledialog ();
When openfiledialog is enabled, the Environment path is changed, which sometimes causes unexpected problems. For exampleProgramA file or database operation in the path changes the path after openfiledialog, and the target file/database cannot be found.
You can call
Environment. currentdirectory = appdomain. currentdomain. basedirectory. tostring ();
Restore path
BTW, ref someone else's blog
C # obtain the path of the current application
For Windows and Web applications, the path they run is different, so the key is to determine which program is currently running. So we can use the following code:
String Path = "";
If (system. environment. currentdirectory = appdomain. currentdomain. basedirectory) // Windows applications are equal
{
Path = appdomain. currentdomain. basedirectory;
}
Else
{
Path = appdomain. currentdomain. basedirectory + "bin \";
}
In this way, if we write a class library and use assembly. loadfrom in the class library, because it is a general class library, it may be used in Windows programs or in the web, so the aboveCodeIt is very convenient.