保存文件时出现 UnauthorizedAccessException,但可以创建目录

编程入门 行业动态 更新时间:2024-10-18 00:20:33
本文介绍了保存文件时出现 UnauthorizedAccessException,但可以创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将文件保存到磁盘,但出现 UnauthorizedAccessException.错误提示我必须获得对该文件夹的正确权限,并且我已经尝试了我能找到的所有可能的用户,但它不起作用.

I'm trying to save a file to the disk but I get UnauthorizedAccessException. The error says I have to get the right permissions on the folder, and I've tried every possible user I can find but it doesn't work.

尝试了以下用户

  • 网络
  • 网络服务
  • IUSR
  • IUSR_[计算机名]

并在没有它的情况下获得全部权利.

And given the full rights without it working.

我发现真正奇怪的是,我在尝试保存文件之前创建了一个目录,并且效果很好,当我尝试将文件保存到该新目录时,我收到了 UnautorhizedAccessException.

What I find really strange is that I create a directory before I try to save the file and that works perfectly, it's when trying to save a file to that new directory that I get the UnautorhizedAccessException.

代码如下:

[HttpPost] public ActionResult Images(HttpPostedFileBase file, string boatId) { if (file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/Content/Images/" + boatId)); Directory.CreateDirectory(path); file.SaveAs(path); } return View($"Filen på {boatId} har laddats upp"); }

对我缺少的东西有什么想法吗?

Any ideas at what I'm missing?

推荐答案

结果我试图做的是保存文件夹而不是文件,我忘记将文件名与路径结合起来.

Turns out what I was trying to do was saving the folder and not the file, I forgot to combine the fileName with the path.

将保存部分更改为以下内容:

Changed the Save part to the following:

file.SaveAs(Path.Combine(path, fileName));

这为我解决了整个问题.

Which solved the whole thing for me.

更多推荐

保存文件时出现 UnauthorizedAccessException,但可以创建目录

本文发布于:2023-11-28 15:19:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642931.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:保存文件   目录   UnauthorizedAccessException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!