Directory.GetCurrentDirectory()没有返回正确的目录 [英] Directory.GetCurrentDirectory() doesn't return the correct directory

查看:144
本文介绍了Directory.GetCurrentDirectory()没有返回正确的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET Core 2.2 MVC服务器中,我想添加一个文件夹来存储静态文件.我找到了以下代码来执行该操作:

In my ASP.NET Core 2.2 MVC server, I want to add a folder to store static files. I found the following code to perform that:

app.UseStaticFiles(new StaticFileOptions {
    FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), "StaticFiles")),
    RequestPath = "/StaticFiles"
});

在我的计算机中,项目位于"C:\ Users \ MyUsername \ source \ repos \ WebApplication1 \ WebApplication1" 中,在同一目录中有一个名为"StaticFiles"的文件夹,上面的代码写入的 Startup.cs 文件.

In my computer, the project is located in "C:\Users\MyUsername\source\repos\WebApplication1\WebApplication1", in the same directory there is a folder called "StaticFiles" and a Startup.cs file where the code above is written.

但是当我执行代码时,出现以下错误:

But when I execute the code I got the following error:

'System.IO.DirectoryNotFoundException' in Microsoft.Extensions.FileProviders.Physical.dll C:\Program Files\IIS Express\StaticFiles\

Directory.GetCurrentDirectory()返回的是"C:\\ Program Files \\ IIS Express" ,而不是"C:\ Users \ MyUsername \源\ repos \ WebApplication1 \ WebApplication1" .如何获得正确的方向?

The Directory.GetCurrentDirectory() is returning "C:\\Program Files\\IIS Express" instead of "C:\Users\MyUsername\source\repos\WebApplication1\WebApplication1". How can I get the correct direction?

推荐答案

这是 ASP.NET Core 2.2 中的一个错误,该错误已在

This is a bug in ASP.NET Core 2.2 which has been reported in GitHub and Microsoft ASP.NET Core team has provided a solution as follows and they will add this solution to the feature release of ASP.NET Core.

编写如下的帮助器类:

public class CurrentDirectoryHelpers
{
    internal const string AspNetCoreModuleDll = "aspnetcorev2_inprocess.dll";

    [System.Runtime.InteropServices.DllImport("kernel32.dll")]
    private static extern IntPtr GetModuleHandle(string lpModuleName);

    [System.Runtime.InteropServices.DllImport(AspNetCoreModuleDll)]
    private static extern int http_get_application_properties(ref IISConfigurationData iiConfigData);

    [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
    private struct IISConfigurationData
    {
        public IntPtr pNativeApplication;
        [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]
        public string pwzFullApplicationPath;
        [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]
        public string pwzVirtualApplicationPath;
        public bool fWindowsAuthEnabled;
        public bool fBasicAuthEnabled;
        public bool fAnonymousAuthEnable;
    }

    public static void SetCurrentDirectory()
    {
        try
        {
            // Check if physical path was provided by ANCM
            var sitePhysicalPath = Environment.GetEnvironmentVariable("ASPNETCORE_IIS_PHYSICAL_PATH");
            if (string.IsNullOrEmpty(sitePhysicalPath))
            {
                // Skip if not running ANCM InProcess
                if (GetModuleHandle(AspNetCoreModuleDll) == IntPtr.Zero)
                {
                    return;
                }

                IISConfigurationData configurationData = default(IISConfigurationData);
                if (http_get_application_properties(ref configurationData) != 0)
                {
                    return;
                }

                sitePhysicalPath = configurationData.pwzFullApplicationPath;
            }

            Environment.CurrentDirectory = sitePhysicalPath;
        }
        catch
        {
            // ignore
        }
    }
}

然后在您的代码中调用 SetCurrentDirectory()方法,如下所示:

Then call the SetCurrentDirectory() method in your code as follows:

app.UseStaticFiles(new StaticFileOptions {
    FileProvider = new PhysicalFileProvider(

        CurrentDirectoryHelpers.SetCurrentDirectory(); // call it here

        Path.Combine(Directory.GetCurrentDirectory(), "StaticFiles")),
    RequestPath = "/StaticFiles"
});

现在一切正常!

这篇关于Directory.GetCurrentDirectory()没有返回正确的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆