开发者

Getting access denied when try to delete file

开发者 https://www.devze.com 2023-01-26 17:15 出处:网络
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 f

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);


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

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?


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


You are opening

reader = new StreamReader(path + fileName);

But you are closing

File.Delete(path);
0

精彩评论

暂无评论...
验证码 换一张
取 消