"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Saturday, May 8, 2010

Deleting a file from the server

hi Everybody,

Some times v may have to replace our old file ( what b the file format) with new one.Just like updating Bio in a job Portal an example. for that v need to find whether it exist a file  On that name if v find. v need to delete it.Some server will done this job automatically some times it may throw an error "Access denied".So let's don't take that risk :)

here is my code


    string imgFolder = Server.MapPath("~/lab/maskemail/img/");
    string[] imgList = Directory.GetFiles(imgFolder, "//specifiey file name.doc");
    foreach (string img in imgList)
    {
        FileInfo imgInfo = new FileInfo(img);
        imgInfo.Delete();
      
    }

Documentaion

To the string imgFolder i'm getting my file path.then i create string array on that string array 'm getting all the file with the file name i specified. after my task is simple. i give the array on a foreach loop 'n i create a new fileinfo for that folder on which my file exist 'n using fileinfo for deleting the file :)


Have a nice day... 'N happy Coding :)
 Illustrated C# 2008 (Windows.Net)Microsoft Visual C# 2008 Step by StepHead First C#

No comments: