001
001

Reputation: 65145

How to run the delete command via Process?

This does not work, it cant find del.exe...

        Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.FileName = "del.exe";
        p.StartInfo.Arguments = "*.bak";
        p.Start();
        p.Close();

Upvotes: 0

Views: 257

Answers (2)

Cody Gray
Cody Gray

Reputation: 244772

You're doing it the wrong way. You should be using the File.Delete method instead.

Sample code:

string sourceDir = @"C:\Backups";   // change this to the location of the files
string[] bakList = Directory.GetFiles(sourceDir, "*.bak");

try
{
    foreach (string f in bakList)
    {
        File.Delete(f);
    }
}
catch (IOException ioex)
{
    // failed to delete because the file is in use
}
catch (UnauthorizedAccessException uaex)
{
    // failed to delete because file is read-only,
    // or user doesn't have permission
}

Upvotes: 3

Brad Christie
Brad Christie

Reputation: 101614

If there a reason you're choosing to execute Process over Directory.GetFiles coupled with File.Delete?

Upvotes: 0

Related Questions