0

So I'm writing code to download some files from a sharepoint, store them locally in a tempFolder, then create a .zip file with these files and then I want to remove the tempFolder & it's files under it. Right now "File.Delete" does that, but all the files are put in my recyclingBin. If this code runs constantly on a server & instead of my now 5 test files, uses hundreds or thousands of files to zip every half hour maybe, it's going to go badly...

So is there a way in C# to hard delete (or shift delete) a folder & it's subfiles?

Tempuslight
  • 1,004
  • 2
  • 17
  • 34
  • 2
    https://stackoverflow.com/questions/19074724/how-do-i-delete-a-folder-and-its-contents-without-putting-them-in-the-recycle 2nd answer – Chris Dixon Apr 25 '18 at 13:07
  • Are you *sure* they are getting sent to the recycle bin? The documentation doesn't say anything about that. This site says the opposite: https://www.dotnetperls.com/file-delete And that site is pretty reliable, in my experience. – Gabriel Luci Apr 25 '18 at 13:13
  • 1
    using (System.IO.File.Create("C:\\Temp\\TestFile.txt")) { } System.IO.File.Delete("C:\\Temp\\TestFile.txt"); leaves no file in the recycle bin. What function are you using? – Chrᴉz remembers Monica Apr 25 '18 at 13:14

1 Answers1

0

You use a CMD workaround for this:

  Process p = new Process();
p.StartInfo = new ProcessStartInfo( "cmd", "/c sdelete -p 1 -s -z -q -a 'path/to/director' )
    {
        RedirectStandardOutput = true,
        UseShellExecute = false,
        CreateNoWindow = true
    };
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
user3127554
  • 521
  • 1
  • 7
  • 28