我正在使用asp.net(C#)4.0进行开发。在上传图片之前,我想要检查上传的文件夹是否存在。如果存在,我需要检查它是否为只读模式,如果是只读模式,我需要将其改为非只读模式。请问如何做到这一点?每次启动应用程序时,文件夹都被设置为只读模式,我希望通过编程来避免这个问题。
我的做法是...
我的做法是...
SaveFilePath = Server.MapPath("~\\_UploadFiles\\") + FileName;
DirectoryInfo oDirectoryInfo = new DirectoryInfo(Server.MapPath("~\\_UploadFiles\\"));
if(!oDirectoryInfo.Exists)
Directory.CreateDirectory(Server.MapPath("~\\_UploadFiles\\"));
else
{
if (oDirectoryInfo.Attributes.HasFlag(FileAttributes.ReadOnly))
{
oDirectoryInfo.Attributes = FileAttributes.Normal;
}
}
if (File.Exists(SaveFilePath))
{
File.Delete(SaveFilePath);//Error is thrown from here
}
这段代码在特定位置抛出错误。文件夹“_UploadFiles”只读,但仍未进入if语句以进行FileAttributes.Normal的操作。
错误信息如下: 拒绝访问路径'C:\Inetpub\wwwroot\WTExpenditurev01_VSS_UploadFiles\Winter.jpg'。