我试图从特定位置删除Excel文件,但无法删除。出现以下错误:
路径'C:\ mypath \ sample.xlsx'的访问被拒绝。
我编写的代码如下:
protected void imgbtnImport_Click(object sender, ImageClickEventArgs e)
{
try
{
string strApplicationPath = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath);
string strXLStoredDirectoryPath = strApplicationPath + "/Information Documents/";
DirectoryInfo di = new DirectoryInfo(strXLStoredDirectoryPath);
string fileName = flUpldSelectFile.FileName;
if (!File.Exists(strXLStoredDirectoryPath))
{
Directory.CreateDirectory(strXLStoredDirectoryPath);
di.Attributes = FileAttributes.Normal;
}
string strCreateXLFileDestinationPath = strXLStoredDirectoryPath + fileName;
if (File.Exists(strCreateXLFileDestinationPath))
{
File.Delete(strCreateXLFileDestinationPath);
}
flUpldSelectFile.SaveAs(strCreateXLFileDestinationPath);
di.Attributes = FileAttributes.ReadOnly;
}
catch (Exception)
{
throw;
}
}
请指导……
-*********************************************************************** 仍然存在问题。它没有得到解决。出现UnauthorizedAccessException错误,因为无法删除文件的访问被拒绝。我已经很累了,请帮忙;我尝试了很多方法……请帮忙。 -*********************************************************************** 这可能是VSS的影响吗?我正在使用它。