| Profiel van MatthewSimplifying ComplicationWeblogLijstenNetwerk | Help |
|
17 april Finding the Assembly locationFinding the location of the running application is a task that is often hit. This is usually required to locate resources relative to the application, such as application settings or deployed files. There are many locations that can be used outside of the assemblies location that are often more appropriate, such as the Users' Application Data directory, temporary directory or a database. One example that I hit recently where those locations just didn't fit the requirement was for an installation deployed configuration file, which is configured by the installer for application wide settings, and was not to be modified once deployed. This method had to be consistent as it would be used in a shared library by WinForms, Web applications and Office Add-ins. Environment.CurrentDirectory, is usually set to the directory the application was started in, so I skipped that and went straight to the method I've used before:
From a WinForms application it returned, as expected:
However, when I was running it in a web application it returned a directory that was not very helpful:
And within an Excel Add-in it returned:
My colleague mentioned another method, so I gave that one a whirl: (Note _Default is the name of a class in the assembly)
Unfortunately it returned the same as above in all instances. After a little searching I found another:
From a WinForm application it returned the same as above. From a Web application hosted by IIS it returned:
Note however, that this also returned a Local Temporary ASP.NET Files location when running under the Visual Studio ASP.NET Development Server, but this is to be expected. From an Excel Add-in it returned:
This method appears to work in WinForm applications, Console applications, Office Add-ins, and Web applications hosted in IIS.
|
|
|