[英]Delete file from folder throws error
我有一个文件夹 G:\\Images,它允许我通过 c# 从上传页面插入图像,当我从文件夹中删除图像时出现问题,我收到以下错误。
System.IO.__Error.WinIOError(Int32 errorCode, String可能FullPath)
在 System.IO.File.InternalDelete(String path, Boolean checkHost)
如果用户想要更新他们的个人资料图片,他们会上传图片,然后我检查数据库中的旧图片,然后在添加新图片 ID 之前使用下面的代码从文件夹中删除它。
但是,如果我上传新图像,然后在发生错误后立即再次上传图像。
我的代码是
if (System.IO.File.Exists(@"G:\\Images\\" + string.Format("{0}.png", OldProfileImage)))
{
try
{
System.IO.File.Delete(@"G:\\Images\\" + string.Format("{0}.png", OldProfileImage));
}
catch(Exception e)
{
logger.Error(string.Format("Delete file error Exception is {0} {1}", e.Source.ToString(), e.StackTrace.ToString()));
}
}
-----------------------按要求更改 e.tostring()----------------
错误是:System.UnauthorizedAccessException:访问路径“G:\\Images\\5bb188f0-2508-4cbd-b83d-9a5fe5914a1b.png”被拒绝。 在 System.IO.__Error.WinIOError(Int32 errorCode, String mightFullPath)
在 System.IO.File.InternalDelete(String path, Boolean checkHost)
但如上所述,我可以插入、删除,但如果我随后插入,则在发生错误后直接删除。
我有一个类似的问题。 我的是一个控制台应用程序。 在我的本地机器上,它工作正常,但是当我放入服务器时,它在System.IO.File.Delete(file);
上给了我这个错误System.IO.File.Delete(file);
澄清一下,我的帐户具有删除权限。 我可以手动删除文件。
后来我用“以管理员身份运行”选项运行程序,它解决了这个问题。
尝试这个:
string file = @"G:\\Images\\" + string.Format("{0}.png", OldProfileImage);
System.IO.File.SetAttributes(file, FileAttributes.Normal);
System.IO.File.Delete(file);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.