【发布时间】:2011-11-22 14:52:07
【问题描述】:
我正在使用 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”。
【问题讨论】: