尝试删除文件时拒绝访问权限

时间:2010-11-25 10:02:10

标签: c# asp.net file access-denied delete-file

我在C:\inetpub\wwwroot\Project\temp\读完文件后尝试删除文件时,我的访问被拒绝了。我已正确关闭()和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);

4 个答案:

答案 0 :(得分:4)

您的File.Delete方法调用应将path + fileName作为参数。这是因为根据此链接http://msdn.microsoft.com/en-us/library/system.io.file.delete.aspx路径是包含文件名的完整路径,而您的路径变量仅包含文件夹名称。

答案 1 :(得分:2)

您要删除File.Delete(path);而不是File.Delete(path + filename);

答案 2 :(得分:1)

您正在打开

reader = new StreamReader(path + fileName);

但你正在关闭

File.Delete(path);

答案 3 :(得分:1)

我也遇到了问题,因此我在这篇帖子上磕磕绊绊地告诉了服务器。我在复制/删除之前和之后添加了以下代码行。

删除

File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);

复制

File.Copy(file, dest, true);
File.SetAttributes(dest, FileAttributes.Normal);

链接:Why is access to the path denied?