我在 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”。
最佳答案
使用System.IO.DirectoryInfo class :
var di = new DirectoryInfo(folderName);
if(di.Exists())
{
if (di.Attributes.HasFlag(FileAttributes.ReadOnly))
{
//IsReadOnly...
}
}
https://stackoverflow.com/questions/7511592/