[英]Getting access denied when try to delete file
每当在C:\\inetpub\\wwwroot\\Project\\temp\\
读取文件后尝试删除文件时,访问都会被拒绝。 我已经正确关闭()和处置()StreamReader吗? 我也对NETWORK SERVICE帐户授予了完全许可? 谁能帮我?
reader = new StreamReader(path + fileName);
DataTable dt = new DataTable();
String line = null;
int i = 0;
while ((line = reader.ReadLine()) != null)
{
String[] data = line.Split(',');
if (data.Length > 0)
{
if (i == 0)
{
dt.Columns.Add(new DataColumn());
foreach (object item in data)
{
DataColumn c = new DataColumn(Convert.ToString(item));
if (Convert.ToString(item).Contains("DATE"))
{
c.DataType = typeof(DateTime);
}
else { c.DataType = typeof(String); }
dt.Columns.Add(c);
}
dt.Columns.Add(new DataColumn("CreatedDate", typeof(DateTime)));
i++;
}
else
{
DataRow row = dt.NewRow();
row[0] = "";
for (int j = 0; j < data.Length; j++)
{
if (dt.Columns[j + 1].DataType == typeof(DateTime))
{
row[j + 1] = Convert.ToDateTime(data[j]);
}
else
{
row[j + 1] = data[j];
}
}
row[data.Length + 1] = DateTime.Now.ToString();
dt.Rows.Add(row);
}
}
}
DataAccess dataAccess = new DataAccess(Constant.CONNECTION_STRING_NAME);
dataAccess.WriteBulkData(dt, Constant.TABLE);
reader.Close();
reader.Dispose();
File.Delete(path);
您的File.Delete方法调用应将路径+ fileName作为参数。 这是因为根据此链接http://msdn.microsoft.com/zh-cn/library/system.io.file.delete.aspx路径是包含文件名的完整路径,而路径变量仅包含文件夹名称。
您正在删除File.Delete(path);
不是File.Delete(path + filename);
我也遇到了问题,因此我在发布到服务器上绊了一下。 我在“复制/删除”之前和之后添加了以下代码行。
删除
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
复制
File.Copy(file, dest, true);
File.SetAttributes(dest, FileAttributes.Normal);
链接: 为什么拒绝访问路径?
你在开
reader = new StreamReader(path + fileName);
但是你要关闭了
File.Delete(path);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.