繁体   English   中英

为什么 AppDomain.CurrentDomain.BaseDirectory 在控制台应用程序中定位 bin catelog?

[英]Why is AppDomain.CurrentDomain.BaseDirectory targeting bin catelog in console application?

我有一个控制台应用程序。 在这个应用程序中,我创建了一个类库项目,我在主项目中引用了它。 在这个类库中,我有一个我正在尝试获取的文件。 代码:

public static string GetPath(string fileName)
{
    char separator = Path.DirectorySeparatorChar;
    string startupPath = AppDomain.CurrentDomain.BaseDirectory;
    string[] pathItems = startupPath.Split(separator);
    string projectPath = string.Join(separator.ToString(),
        pathItems.Take(pathItems.Length - 1)) + ".Handler";
    string path = Path.Combine(projectPath, "WebRequestHandlers\\Files" + separator + fileName);

    return path;
}

我之前在 .net web 项目中使用过它并且它已经工作但由于某种原因这是我的 AppDomain.CurrentDomain.BaseDirectory:

C:\\projects\\OverWatchServerCall\\OverWatchServerCall\\bin\\Debug\\

它应该是:

C:\\projects\\OverWatchServerCall\\OverWatchServerCall\\

该文件在 classlibrary 项目中,正如我之前所说,这在 .net web api 项目中工作正常

为什么会这样?

编辑:

Directory.GetCurrentDirectory()

返回相同的路径

AppDomain.CurrentDomain.BaseDirectory不是由程序集定义的,而是由宿主环境(即应用程序服务)定义的。

构建控制台应用程序后,可执行文件和附属程序集将转到*.csproj文件中定义的输出路径。 默认为bin\\$(Configuration) (例如: bin\\Debug )。

由于整个可执行文件都在输出路径中执行,因此这是基本目录。

为什么在 ASP.NET WebAPI 项目中在您的情况下运行良好? 可能是因为您在 IIS 中托管 ASP.NET WebAPI。 如果您使用托管在 Windows 服务中的 OWIN/Katana 来执行此操作,您最终会遇到同样的问题。

暂无
暂无

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

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