C# 应用程序创建的目录的Unauthorizedaccessexception(IIS锁定文件)
我有一个程序,用户可以上传照片并将其存储在应用程序中(例如~/files/ac9ff990-273c-4fda-a51a-155c54db72ab/original/photo1.jpg)。稍后,用户可以删除该文件。这可以很好地工作,只是当所有子文件和文件夹都被删除时,告知级别的文件夹(这个GUID)仍然存在,并且我会抛出Unauthorizedaccessexception错误。我留下了大量的空文件夹,这在处理1000次上传时真的很烦人 我看不出这怎么会是权限问题,因为权限没有什么奇怪的地方,我在桌面上遇到的问题与在live server上遇到的问题相同。我已经关闭了索引,并尝试在没有任何运气的情况下授予“EVERYONE”权限,并在没有任何运气的情况下将文件夹所有者设置为不同的用户。(我也没有在Windows资源管理器中打开文件夹) 我看不出这怎么会是权限问题,因为.NET应该使用与删除相同的文件夹来创建它。有什么想法吗 公共类文件管理器 { 私有静态ILog log=LogManager.GetLogger(typeof(FileManager)); 公共枚举上载类型:int{ORIGINAL_FILE=1,PROCESSED_FILE=2,associative_FILE=3}C# 应用程序创建的目录的Unauthorizedaccessexception(IIS锁定文件),c#,.net,file-upload,file-io,file-permissions,C#,.net,File Upload,File Io,File Permissions,我有一个程序,用户可以上传照片并将其存储在应用程序中(例如~/files/ac9ff990-273c-4fda-a51a-155c54db72ab/original/photo1.jpg)。稍后,用户可以删除该文件。这可以很好地工作,只是当所有子文件和文件夹都被删除时,告知级别的文件夹(这个GUID)仍然存在,并且我会抛出Unauthorizedaccessexception错误。我留下了大量的空文件夹,这在处理1000次上传时真的很烦人 我看不出这怎么会是权限问题,因为权限没有什么奇怪的地方,
private const string userFilesFolder = "~/files/";
private const string originalSubFolder = "original/";
private const string processedSubFolder = "processed/";
private const string ancillarySubFolder = "ancillary/";
internal static void DeleteJobItemFiles(Guid jobItemId)
{
if (jobItemId == Guid.Empty)
throw new ApplicationException("jobItemId is Empty");
try
{
var directory = GetDirectory(jobItemId);
string routePath = HttpContext.Current.Server.MapPath(HttpUtility.UrlDecode(directory.AbsolutePath));
if (Directory.Exists(routePath))
Directory.Delete(routePath, true);
}
catch (HttpException exception)
{
throw new UploadManagerException(ERROR_CODE.HTTP_CONTEXT_NULL, exception);
}
catch (Exception exception)
{
throw new UploadManagerException(ERROR_CODE.GENERAL_FILE, exception);
}
}
internal static string SaveFile(Guid jobItemId, Stream inputStream, UPLOAD_TYPE uploadType, string fileName)
{
if (jobItemId == Guid.Empty)
throw new ArgumentException("jobItemId is Empty");
if (inputStream == null)
throw new ArgumentNullException("inputStream");
if (fileName == null)
throw new ArgumentNullException("fileName");
if (inputStream.CanSeek)
inputStream.Position = 0;
Regex rgx = new Regex("[^a-zA-Z0-9 -_\\.]");
fileName = rgx.Replace(fileName, "");
fileName = fileName.Replace(" ", "");
var directoryPathUri = GetDirectory(jobItemId, uploadType);
// New file Uri
var fileUri = new Uri(directoryPathUri, fileName);
try
{
string localDirectoryPath = HttpContext.Current.Server.MapPath(HttpUtility.UrlDecode(directoryPathUri.AbsolutePath));
string localFilePath = HttpContext.Current.Server.MapPath(HttpUtility.UrlDecode(fileUri.AbsolutePath));
if (!Directory.Exists(localDirectoryPath))
Directory.CreateDirectory(localDirectoryPath);
using (FileStream outputStream = File.Create(localFilePath))
{
Byte[] buffer = new Byte[256];
inputStream.Position = 0;
int bytesRead;
do
{
bytesRead = inputStream.Read(buffer, 0, buffer.Length);
if (bytesRead > 0)
outputStream.Write(buffer, 0, bytesRead);
}
while (bytesRead > 0);
}
}
catch (IOException exception)
{
log.Error("Unexpected exception in FileManager.SaveFile", exception);
throw new UploadManagerException(ERROR_CODE.FILE_SAVE, exception);
}
catch (HttpException exception)
{
throw new UploadManagerException(ERROR_CODE.HTTP_CONTEXT_NULL, exception);
}
catch (Exception exception)
{
throw new UploadManagerException(ERROR_CODE.GENERAL_FILE, exception);
}
var rootUri = GetDirectory();
return VirtualPathUtility.ToAppRelative(fileUri.AbsolutePath);
}
private static Uri GetDirectory()
{
// Root of site
try
{
return new Uri(HttpContext.Current.Request.Url, HttpContext.Current.Request.ApplicationPath);
}
catch (HttpException exception)
{
throw new UploadManagerException(ERROR_CODE.HTTP_CONTEXT_NULL, exception);
}
}
private static Uri GetDirectory(Guid jobItemId)
{
if (jobItemId == Guid.Empty)
throw new ArgumentException("jobItemId is Empty");
var rootUri = GetDirectory();
//Folder where all files are stored
var userFilesUri = new Uri(rootUri, VirtualPathUtility.ToAbsolute(userFilesFolder));
//Folder for job
return new Uri(userFilesUri, jobItemId.ToString() + "/");
}
private static Uri GetDirectory(Guid jobItemId, UPLOAD_TYPE uploadType)
{
if (jobItemId == Guid.Empty)
throw new ArgumentException("jobItemId is Empty");
string subFolder;
switch (uploadType)
{
case UPLOAD_TYPE.ORIGINAL_FILE:
subFolder = originalSubFolder;
break;
case UPLOAD_TYPE.PROCESSED_FILE:
subFolder = processedSubFolder;
break;
case UPLOAD_TYPE.ANCILLARY_FILE:
subFolder = ancillarySubFolder;
break;
default:
throw new NotImplementedException();
}
//Folder for file
return new Uri(GetDirectory(jobItemId), subFolder);
}
}
重要更新:这似乎是由IIS锁定文件或类似原因造成的。我试图删除的图像显示在页面上。当WCF呼叫进来删除页面时,文件夹无法删除。如果我编辑HTML使图像不显示,则可以删除文件夹。对任何人都有意义吗?我感觉不到帽子某些非托管项没有被正确清理。如果你制作一个小得多的应用程序来显示问题,这会有很大帮助。我会这样做。我尝试删除了调用file1.Delete()和dir1.Delete()的递归函数的Directory.Delete(path,true)。dir1.Delete()很有趣对于原始文件夹,在guid文件夹尝试删除但失败之前,似乎无效:/guid/original/photo1.jpg
private static void RecursiveDelete(DirectoryInfo dirInfo){foreach(dirInfo.GetDirectories()中的var目录){RecursiveDelete(directory);}foreach(dirInfo.GetFiles()中的var文件){file.Delete();}dirInfo.Delete();}
我的小应用程序运行良好,表明这段代码没有任何问题。有没有关于其他地方可能存在什么样的问题可能导致这一问题的想法?