3

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);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Leo
  • 2,173
  • 6
  • 28
  • 37
  • 1
    As Microgen has said, its probably still in use. In the past I've had problems where it takes a little bit of time to properly release the file, try putting a delay in beofre you try and delete it to see if it is that – Iain Ward Nov 25 '10 at 10:12

4 Answers4

4

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?

Community
  • 1
  • 1
Durgesh Pandey
  • 2,314
  • 4
  • 29
  • 43
  • While the OP was doing something silly i found this because access to the file i was trying to write to was actually denied. THIS fixed it. ``` File.SetAttributes(file, FileAttributes.Normal); ``` – Arachnid Apr 30 '19 at 03:26
4

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.

Mamta D
  • 6,310
  • 3
  • 27
  • 41
2

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

John Warlow
  • 2,922
  • 1
  • 34
  • 49
1

You are opening

reader = new StreamReader(path + fileName);

But you are closing

File.Delete(path);
Jens
  • 3,249
  • 2
  • 25
  • 42