Surya sasidhar
Surya sasidhar

Reputation: 30323

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

Upvotes: 0

Views: 606

Answers (5)

MRG
MRG

Reputation: 3219

Your code should be as below :

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

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

Upvotes: 1

KV Prajapati
KV Prajapati

Reputation: 94653

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

Upvotes: 3

Francis B.
Francis B.

Reputation: 7208

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

Upvotes: 6

Avinash
Avinash

Reputation: 3291

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

Upvotes: 1

Jay Riggs
Jay Riggs

Reputation: 53603

Your use of FileInfo.Delete takes no arguments.

You want something like:

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

Upvotes: 3

Related Questions