尝试删除文件时访问被拒绝
每当我在 C:\inetpub\wwwroot\Project\temp\
读取完文件后尝试删除该文件时,访问都会被拒绝。我已经正确 Close() 和 Dispose() 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);
I'm getting access denied whenever I try to delete a file after finishing reading it at C:\inetpub\wwwroot\Project\temp\
. I Close() and Dispose() the StreamReader properly already? I also gave full permission for NETWORK SERVICE account? Can anyone help me?
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的 File.Delete 方法调用应采用路径 + 文件名作为参数。这是因为根据此链接 http://msdn。 microsoft.com/en-us/library/system.io.file.delete.aspx 路径是包含文件名的完整路径,路径变量仅包含文件夹名称。
Your File.Delete method call should take path + fileName as parameter. This is because according to this link http://msdn.microsoft.com/en-us/library/system.io.file.delete.aspx path is the full path including the filename and your path variable includes only the folder name.
我也遇到了这个问题,因此我偶然发现了服务器上的这篇文章。我在复制/删除之前和之后添加了以下代码行。
删除
复制
链接:为什么访问该路径被拒绝?
I also had the problem, hence me stumbling on this post to server. I added the following line of code before and after a Copy / Delete.
Delete
Copy
Link: Why is access to the path denied?
您正在删除
File.Delete(path);
而不是File.Delete(path + filename);
You're deleting
File.Delete(path);
notFile.Delete(path + filename);
你正在打开
但你正在关闭
You are opening
But you are closing