-2

I'm trying to make an updater in C# and I want to delete the updater once it's done.
So I have this code in C#:

var path = Environment.CurrentDirectory + "\\WindowsFormsApplication1.exe";
Process.Start("cmd.exe /c del " + path);

But I get this error message:

Win32Exception was unhandled
The system cannot find the file specified

But I'm sure the path is spelled right, so I don't think that's the problem.

Any ideas?

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Yuki Kutsuya
  • 3,848
  • 10
  • 45
  • 60
  • 2
    Is there a reason you can't use the File.Delete() static method? http://msdn.microsoft.com/en-us/library/system.io.file.delete.aspx – SupremeDud Apr 17 '12 at 14:45
  • 1
    Try printing out path. I think you'll be surprised at what you see. – Neil Apr 17 '12 at 14:45
  • 3
    As an aside, you should consider using `System.IO.Path.Combine` to build path strings instead of using straight string concats. – M.Babcock Apr 17 '12 at 14:47
  • 1
    Exact duplicate of http://stackoverflow.com/questions/1068846/delete-currently-loaded-assembly – Andrew T Finnell Apr 17 '12 at 14:48

2 Answers2

3
var path = Environment.CurrentDirectory + "\\WindowsFormsApplication1.exe";

Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = string.Format("/c del \"{0}\"", path);
process.Start();    

Or

Process.Start("cmd.exe", string.Format("/c del \"{0}", path));
Uwe Keim
  • 38,279
  • 56
  • 171
  • 280
Shawn C.
  • 6,068
  • 3
  • 33
  • 38
0

This question was answered here: Run Command Prompt Commands

Aditional solution is to build a BAT file and inside it to call del.

Community
  • 1
  • 1
Zelter Ady
  • 6,118
  • 12
  • 46
  • 71