找不到ASP.net路径的一部分

问题描述 投票:-1回答:3

我已经建立了一个网站,其中有一个名为Zipfile的文件夹,我想从中访问LSC.exe文件进行下载。当我点击路径时它会给出以下异常System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files\IIS Express\~\Zipfile\LSC.exe'. 请给我任何建议。

调节器

[HttpGet]
public FileResult downloadFile()
{
    byte[] fileBytes = System.IO.File.ReadAllBytes("~/Zipfile/LSC.exe");
    string fileName = "SkypeSetup.exe";
    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
c# asp.net
3个回答
1
投票

您需要添加Server.MapPath(exe路径)以获取映射到其相对路径

  byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath("~/Zipfile/LSC.exe"));

1
投票

您应该使用Server.MapPath映射到应用程序目录下的文件。

参考:HttpServerUtility.MapPath Method


1
投票

试试以下代码:

public FileResult downloadFile()
    {
     byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath("~/Zipfile/LSC.exe"));
        string fileName = "SkypeSetup.exe";
        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
    }
© www.soinside.com 2019 - 2024. All rights reserved.