5

i write the code in asp.net using c# to delete the file in my computer, but it is not deleting please help me thank u. this is my code, i write in button click event

        string path = "E:\\sasi\\delt.doc";
        FileInfo myfileinf = new FileInfo(path);
        myfileinf.Delete();
4
  • 1
    1) You aren't trying to delete a file in the client side using ASP.NET code are you? 2) Checking myfileInf.Exists would help. 3) You should show us the Delete exception because I don't think it's not throwing one Commented Nov 19, 2009 at 10:03
  • ya i am trying to delete a file Commented Nov 19, 2009 at 10:48
  • Surya, ClientSide or ServerSide? You keep us in the dark. Commented Nov 19, 2009 at 10:55
  • sorry Mr. Henk Holterman i want to delete server side Commented Nov 19, 2009 at 11:12

3 Answers 3

7
public void DeleteFileFromFolder(string StrFilename)
{

    string strPhysicalFolder = Server.MapPath("..\\");

    string strFileFullPath = strPhysicalFolder + StrFilename;

    if (IO.File.Exists(strFileFullPath)) {
        IO.File.Delete(strFileFullPath);
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

I converted it to VB, and it worked like a charm. Thank you, nearly 13 years later.
3

In order to delete a file you must ensure that the account has sufficient permissions. In general ASP.NET applications run under limited permission account such as Network Service. For example if your application runs under IIS 6 you could go to the Administration Console and set a custom account in the application pool properties:

alt text http://i.msdn.microsoft.com/Bb969101.SharePoint_SQL_TshootingFig3%28en-US,SQL.90%29.jpg

You need to ensure that the account is member of the IIS_WPG group.

1 Comment

Where is this located in IIS 8
2

Make sure the ASP user has permissions to this folder. By default this user is not given access to much of the harddrive..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.