简体   繁体   中英

Deleting a file in asp.net

I write the code below to delete a file:

FileInfo file = new FileInfo(filename);
file.Delete(Path);

but I am getting the error that file.Delete(path) takes 1 argument please help me

The method Delete of FileInfo does not accept any parameter, so you need to write your code like this:

FileInfo file = new FileInfo(filename); 
file.Delete();

Your use of FileInfo.Delete takes no arguments.

You want something like:

FileInfo file = new FileInfo(filename); 
file.Delete();

You are creating an instance of FileInfo having a filename as an arguement. Method file.Delete() will remove the file which you passed through a constructor. In fact, the argument of constructor must be an absolute path along with filename.

String filename=@"c:\xyz\aa.txt";
FileInfo file=new FileInfo(filename);
file.Delete();

try this

   if (System.IO.File.Exists(path))
            {
                System.IO.FileInfo info = new System.IO.FileInfo(path);
                System.IO.File.SetAttributes(info.FullName,     
                                       System.IO.FileAttributes.Normal);
                System.IO.File.Delete(info.FullName);
            }

Your code should be as below :

FileInfo file = new FileInfo(filename);
file.Delete();

The Delete method of FileInfo object does not take any arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM