我正在asp.net(C#)4.0中工作。在上传图片之前,我想检查一下图片上传的文件夹是否存在。如果它存在,它是否是只读的,如果它是只读的,我想使它不是只读的。我该怎么做。每次启动我的应用程序时,该文件夹都设置为只读。所以我想通过编程检查全部来避免这个问题。检查文件夹是否只读在C#中.net
我不喜欢这样...
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 ' 被拒绝。
可能重复(http://stackoverflow.com/questions/2316308/remove-readonly-of-folder-from-c) – onof