尝试删除文件时访问被拒绝

发布于 2024-10-04 08:03:33 字数 2256 浏览 0 评论 0原文

每当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

桃酥萝莉 2024-10-11 08:03:33

您的 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.

镜花水月 2024-10-11 08:03:33

我也遇到了这个问题,因此我偶然发现了服务器上的这篇文章。我在复制/删除之前和之后添加了以下代码行。

删除

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

复制

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

链接:为什么访问该路径被拒绝?

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

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

Copy

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

Link: Why is access to the path denied?

丘比特射中我 2024-10-11 08:03:33

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

You're deleting File.Delete(path); not File.Delete(path + filename);

ぇ气 2024-10-11 08:03:33

你正在打开

reader = new StreamReader(path + fileName);

但你正在关闭

File.Delete(path);

You are opening

reader = new StreamReader(path + fileName);

But you are closing

File.Delete(path);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文