简体   繁体   English

我可以使用反射来查找ASP.NET中的bin / [Configuration]文件夹而不是asp临时文件夹

[英]Can I use reflection to find the bin/[Configuration] folder in ASP.NET instead of the asp temporary folder

I have an ASP.NET website and I want to find the /bin/[Configuration] folder to use an external tool (an exe file). 我有一个ASP.NET网站,我想找到/ bin / [Configuration]文件夹来使用外部工具(exe文件)。 When I use reflection to get calling assemblies location it returns something similar to: 当我使用反射获取调用程序集位置时,它返回类似于:

C:\\Windows\\Microsoft.NET\\Framework\\\\...\\Temporary ASP.NET Files\\a1388a5e\\\\...\\my.dll

Since each dll has its own directory under the temp ASP.NET Files this fails for me. 由于每个dll在临时ASP.NET文件下都有自己的目录,因此对我来说失败了。

How can I get the location of the compiled binary folder where the dll's and the .exe is (ie bin/) instead of asp.net's temporary cache? 如何获取已编译的二进制文件夹的位置,其中dll和.exe是(即bin /)而不是asp.net的临时缓存?

Notes 笔记

  • This code is in a supporting library that can be called from ASP.NET websites or other console/windows apps. 此代码位于支持库中,可以从ASP.NET网站或其他控制台/ Windows应用程序调用。

You could try (taken from How to: Get the Application Directory ): 您可以尝试(取自如何:获取应用程序目录 ):

Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

I set up a quick test with: 我设置了一个快速测试:

  • A Web Application project running under the VS dev server (compiled site, /bin directory, etc). 在VS dev服务器(已编译的站点,/ bin目录等)下运行的Web应用程序项目。
  • A class library project, referenced by the WA. 由WA引用的类库项目。
  • In the web application I created a page that called the following method from the library project: 在Web应用程序中,我创建了一个从库项目调用以下方法的页面:

     using System.IO; using System.Reflection; namespace TestHelpers { public class ClassHelpers { public static string PathToBin() { return Path.GetDirectoryName( Assembly.GetExecutingAssembly().GetName().CodeBase); } } } 

This resulted in the following output on the page: 这导致页面上的以下输出:

file:\\C:\\Users\\ UserName \\Documents\\Visual Studio 2008\\Websites\\ Solution \\ Project \\bin file:\\ C:\\ Users \\ UserName \\ Documents \\ Visual Studio 2008 \\ Websites \\ Solution \\ Project \\ bin

Which is what I'd expect. 这是我所期待的。

使用Server.Mappath( “〜\\ BIN”)

Any reason not to just do 任何理由不做

Server.MapPath("~/bin");

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM