【发布时间】:2010-02-23 14:06:48
【问题描述】:
我正在尝试从特定位置删除 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的影响吗?我正在使用它
【问题讨论】:
-
您的文件系统设置是否正确?您必须设置它们才能执行删除等操作。
-
我们需要知道这是否是一个 asp.net 应用程序。
-
您是否尝试过使用我建议的工具来查看文件是否被锁定?还有什么会创建这个文件?如果是同一个应用程序,请显示创建/保存文件的代码。我已经遇到过很多次并且能够相当快地诊断它
-
啊,匈牙利符号!我的眼睛!