我有一个文件夹G:\ image,它允许我通过c#从上传页面插入图像,当我从文件夹中删除图像时会出现以下错误。
System.IO.__Error.WinIOError(Int32 errorCode,String maybeFullPath) 在System.IO.File.InternalDelete(字符串路径,布尔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()));
}
}错误是:拒绝对路径'G:\Images\5bb188f0-2508-4cbd-b83d-9a5fe5914a1b.png‘的System.UnauthorizedAccessException:访问。在System.IO.__Error.WinIOError(Int32 errorCode,String maybeFullPath) 在System.IO.File.InternalDelete(字符串路径,布尔checkHost)
但如前所述,我可以插入、删除,但如果插入,则在错误发生后直接删除。
发布于 2020-08-25 10:18:05
我也有过类似的问题。我的是一个控制台应用程序。在我的本地机器中,它工作得很好,但是当我进入服务器时,它会在System.IO.File.Delete(file);上把这个错误告诉我。
为了澄清,我的帐户有删除权限。我可以手动删除文件。
稍后,我使用“”选项运行程序,它解决了这个问题。
发布于 2014-09-12 13:56:29
试试这个:
string file = @"G:\\Images\\" + string.Format("{0}.png", OldProfileImage);
System.IO.File.SetAttributes(file, FileAttributes.Normal);
System.IO.File.Delete(file);https://stackoverflow.com/questions/25809383
复制相似问题