【问题标题】:How to create a virtual directory in IIS Express using C#如何使用 C# 在 IIS Express 中创建虚拟目录
【发布时间】:2020-07-26 08:26:48
【问题描述】:

您好,我的控制器中有一个方法,如下所示。

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult UploadImage(int? id)
        {
            if (id == null)
                return HttpNotFound();

            Component c = db.Components.Find((int)id);
            HttpPostedFileBase photo = Request.Files["image"];

            if (photo != null && photo.ContentLength > 0)
            {
                var filename = IGT.imagePath + "\\Components\\" + id.ToString() + ".jpg";

                photo.SaveAs(filename);
                c.Image_Url = IGT.baseUrl + "/Content/images/Components/" + id.ToString() + ".jpg";
                db.SaveChanges();
            }
            else
            {
                if (Request["imageurl"] != null && Request["imageurl"].Length > 0)
                {
                    // download this file
                    WebClient wc = new WebClient();
                    wc.DownloadFile(Request["imageurl"], IGT.imagePath + "\\Components\\" + id.ToString() + ".jpg");
                    c.Image_Url = IGT.baseUrl + "/Content/images/Components/" + id.ToString() + ".jpg";
                    db.SaveChanges();
                }

            }
            HttpPostedFileBase reference = Request.Files["referencefile"];

            if (reference != null && reference.ContentLength > 0)
            {
                // Upload the origin file and create a URL 
                var filename = IGT.contentPath + "\\uploads\\Comp-" + id.ToString() + "-" + System.IO.Path.GetFileName(reference.FileName);
                reference.SaveAs(filename);
                c.Reference_Url = IGT.baseUrl + "/Content/uploads/Comp-" + id.ToString() + "-" + System.IO.Path.GetFileName(reference.FileName);
                db.SaveChanges();

            }

            return RedirectToAction("Edit", new { id = id });
        }

但目前当它到达时

photo.SaveAs(filename);

我收到错误消息

System.IO.DirectoryNotFoundException: '找不到路径的一部分'C:\Users\chris\Source\Repos\inventory2.0\PIC_Program_1.0\Content\images\Components\498.jpg

如何创建一个 try catch 块,以便如果 IIS Express 中不存在该文件夹,它会创建它?

【问题讨论】:

    标签: c# asp.net-mvc iis error-handling try-catch


    【解决方案1】:

    您可以使用以下代码以编程方式创建目录:

     if (!Directory.Exists(appDataPath)) {
         Directory.CreateDirectory(appDataPath);
     }
    

    并使用directory.SetAccessControl(security); 方法设置该文件夹的权限。

    更多详情请参考以下链接:

    https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.createdirectory?view=netframework-4.8

    C# Creating directory and setting the permissions

    https://www.kunal-chowdhury.com/2016/02/folder-permission.html

    【讨论】:

      【解决方案2】:

      你能试试这个代码吗:

      if (!System.IO.Directory.Exists("your folder")) {
                      System.IO.Directory.CreateDirectory("Your Folder");
                  }
      

      另外,请确保您的 IIS 用户对该文件夹目录具有读/写权限。

      【讨论】:

        猜你喜欢
        • 2012-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-02
        • 1970-01-01
        • 2018-02-26
        • 2012-11-02
        • 1970-01-01
        相关资源
        最近更新 更多