Hi all,
I am getting this UnauthorisedAccess Exception when I try to delete a file using the File.Delete() method...I hav permitted full control to NETWORK SERVICES, IUSR_(user name) and ASP.NET Machine Account on the folder that contains the file to be deleted..i'm stumped..i even tried impersonating user (administrator) in the web.config file but nothing seems to be able to budge the exception...
My page also contains a file upload module that enables users to upload file(to the same folder) and that part works perfectly fine..when i try to delete the same file..it says access is denied to the path(full path upto the folder in question)...pls do help all ya brilliant folks out there...i cant blv this error has wasted my entire day already...

Thnx in adv
inAweOfOOP.

Hi all,
I am getting this UnauthorisedAccess Exception when I try to delete a file using the File.Delete() method...I hav permitted full control to NETWORK SERVICES, IUSR_(user name) and ASP.NET Machine Account on the folder that contains the file to be deleted..i'm stumped..i even tried impersonating user (administrator) in the web.config file but nothing seems to be able to budge the exception...
My page also contains a file upload module that enables users to upload file(to the same folder) and that part works perfectly fine..when i try to delete the same file..it says access is denied to the path(full path upto the folder in question)...pls do help all ya brilliant folks out there...i cant blv this error has wasted my entire day already...

Thnx in adv
inAweOfOOP.

Can you please post some line of code that you have implemented and giving error ? also can you please make sure that file is not used by any other process ?

Hi all,
I am getting this UnauthorisedAccess Exception when I try to delete a file using the File.Delete() method...I hav permitted full control to NETWORK SERVICES, IUSR_(user name) and ASP.NET Machine Account on the folder that contains the file to be deleted..i'm stumped..i even tried impersonating user (administrator) in the web.config file but nothing seems to be able to budge the exception...
My page also contains a file upload module that enables users to upload file(to the same folder) and that part works perfectly fine..when i try to delete the same file..it says access is denied to the path(full path upto the folder in question)...pls do help all ya brilliant folks out there...i cant blv this error has wasted my entire day already...

Thnx in adv
inAweOfOOP.

Hi, this is the problem when the file is open on network. File.Delete doesn't work and through authorization error at this time. For this try to use batch file for deletion.

I guess u have tested the deletion case in debug mode.For that the file remained open for deletion first time and did't close, so the file access error. Do run the code with a fresh file after freshly starting the application.
Frankly speaking, i too had similar file denied problems several times, whereas i know the code is perfectly all rite.The more strange part is that it works perfectly rite when uploaded to server,do try it,i guess it will be same for ur part.

protected void Button1_Click(object sender, EventArgs e)
    {
        string fileToDelete = "";
        int rtn = 0;
        try
        {
            foreach (GridViewRow r in GridView1.Rows)
            {
                CheckBox check = (CheckBox)r.FindControl("CheckBox1");
                if (check.Checked)
                {
                    [B]LinkButton article_name = (LinkButton)r.FindControl("LinkButton1");
                    fileToDelete = article_name.PostBackUrl.ToString();
                    fileToDelete = Path.Combine(Server.MapPath(path), fileToDelete);
                    File.Delete(fileToDelete);[/B]
                    OleDbCommand cmd = new OleDbCommand("delete from psm_competitor_articles where caption = '" + article_name.Text + "'", con);
                    con.Open();
                    rtn += cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
            load();
            StatusLabel.Text = rtn + " files deleted from Database.";
        }
        catch (OleDbException ole_e)
        {
            StatusLabel.Text = "Upload status: The file could not be deleted. The following error occured: " + ole_e.Message;
        }
    }

Above is the code in my page meant for deletion(text in bold is the code concerned with file deletion)...i have'nt yet felt that the problem lies in the code but then i'm still a rookie...pls help folks...

Hi, this is the problem when the file is open on network. File.Delete doesn't work and through authorization error at this time. For this try to use batch file for deletion.

Sir I don't know how to use batch file for deletion...pls do help me..

Hi, this is the problem when the file is open on network. File.Delete doesn't work and through authorization error at this time. For this try to use batch file for deletion.

Sir I don't know how to use batch file for deletion...pls do help me..

Hi, this is the problem when the file is open on network. File.Delete doesn't work and through authorization error at this time. For this try to use batch file for deletion.

Sir I don't know how to use batch file for deletion...pls do help me..

Sir I don't know how to use batch file for deletion...pls do help me..

private void deleteFile(string filepath)
        {
            StreamWriter sw = new StreamWriter("d:\\test.bat");
            sw.Write("del "+filepath);
            sw.Close();
            System.Diagnostics.ProcessStartInfo p = new System.Diagnostics.ProcessStartInfo(@"d:\test.bat");
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo = p;
            proc.Start();
            proc.WaitForExit();
        }

This is the right way. Try and let me know.

private void deleteFile(string filepath)
        {
            StreamWriter sw = new StreamWriter("d:\\test.bat");
            sw.Write("del "+filepath);
            sw.Close();
            System.Diagnostics.ProcessStartInfo p = new System.Diagnostics.ProcessStartInfo(@"d:\test.bat");
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo = p;
            proc.Start();
            proc.WaitForExit();
        }

This is the right way. Try and let me know.

Thnx a bundle murugavel Sir..!! you saved me...it worked perfectly I will mark this thread as solved.

protected void Button1_Click(object sender, EventArgs e)
    {
        string fileToDelete = "";
        int rtn = 0;
        try
        {
            foreach (GridViewRow r in GridView1.Rows)
            {
                CheckBox check = (CheckBox)r.FindControl("CheckBox1");
                if (check.Checked)
                {
                    [B]LinkButton article_name = (LinkButton)r.FindControl("LinkButton1");
                    fileToDelete = article_name.PostBackUrl.ToString();
                    fileToDelete = Path.Combine(Server.MapPath(path), fileToDelete);
                    File.Delete(fileToDelete);[/B]
                    OleDbCommand cmd = new OleDbCommand("delete from psm_competitor_articles where caption = '" + article_name.Text + "'", con);
                    con.Open();
                    rtn += cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
            load();
            StatusLabel.Text = rtn + " files deleted from Database.";
        }
        catch (OleDbException ole_e)
        {
            StatusLabel.Text = "Upload status: The file could not be deleted. The following error occured: " + ole_e.Message;
        }
    }

Above is the code in my page meant for deletion(text in bold is the code concerned with file deletion)...i have'nt yet felt that the problem lies in the code but then i'm still a rookie...pls help folks...

What the value of PostBackUrl and Path in below two statements ?
fileToDelete = article_name.PostBackUrl.ToString();
fileToDelete = Path.Combine(Server.MapPath(path), fileToDelete);

Also once this two steps get executes what the value of fileToDelete ? it maybe possible value of fileToDelete is wired and causes the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.