c# - permanently - vb net delete file




How do you place a file in recycle bin instead of delete? (6)

http://www.daveamenta.com/2008-05/c-delete-a-file-to-the-recycle-bin/

From above:

using Microsoft.VisualBasic;

string path = @"c:\myfile.txt";
FileIO.FileSystem.DeleteDirectory(path, 
    FileIO.UIOption.OnlyErrorDialogs, 
    RecycleOption.SendToRecycleBin);

Programmatic solution of course...


From MSDN:

Add a reference to Microsoft.VisualBasic assembly. The needed class is found in this library.

Add this using statement to the top of the file using Microsoft.VisualBasic.FileIO;

Use FileSystem.DeleteFile to delete a file, it has the option to specify recycle bin or not.

Use FileSystem.DeleteDirectory to delete a directory with the option to specify to send it to the recycle bin or not.


Use FileSystem.DeleteFile and specify the right RecycleOption.

While this will work with UI Interactive Apps, it will not work with non UI interactive apps like a Windows Service app.


Use Directory.Delete, if you want to delete the directory directly, without using the recycle bin, as your DeleteDirectory method does. To use the recycle bin, stick with the solution provided in the question you linked.


You need to delve into unmanaged code. Here's a static class that I've been using:

public static class Recycle
{
    private const int FO_DELETE = 3;
    private const int FOF_ALLOWUNDO = 0x40;
    private const int FOF_NOCONFIRMATION = 0x0010;

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
    public struct SHFILEOPSTRUCT
    {
        public IntPtr hwnd;
        [MarshalAs(UnmanagedType.U4)]
        public int wFunc;
        public string pFrom;
        public string pTo;
        public short fFlags;
        [MarshalAs(UnmanagedType.Bool)]
        public bool fAnyOperationsAborted;
        public IntPtr hNameMappings;
        public string lpszProgressTitle;
    }

    [DllImport("shell32.dll", CharSet = CharSet.Auto)]
    static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);

    public static void DeleteFileOperation(string filePath)
    {
        SHFILEOPSTRUCT fileop = new SHFILEOPSTRUCT();
        fileop.wFunc = FO_DELETE;
        fileop.pFrom = filePath + '\0' + '\0';
        fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;

        SHFileOperation(ref fileop);
    }
}

Addendum:

  • Tsk tsk @ Jeff for "using Microsoft.VisualBasic" in C# code.
  • Tsk tsk @ MS for putting all the goodies in VisualBasic namespace.

Microsoft.VisualBasic.FileIO.FileSystem equivalence in C#

The same/similar functionality is available within the System.IO namespace:

System.IO.FileInfo fi = new System.IO.FileInfo("C:\\Test.txt");
fi.Delete();

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("C:\\Test");
di.Delete(true); //Recursive, pass false for no recursion.

I'm not aware of existing SendToRecycleBin equivalent, but you could try:

di.MoveTo("C:\\$Recycle.Bin\\S-..."); //You'd need to know the SID of the user logged in

To replicate the example
The following code will give you something similar to what you have provided as your example:

try
{
    bool deletePermanently = true; //Set to false to move

    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("C:\\Test");
    if (deletePermanently)
    {
        if (di.Exists)
            di.Delete(true);
    }
    else
    {
        if (di.Exists)
            di.MoveTo("C:\\$Recycle.Bin\\S-0-0-00-00000000-000000000-0000000000-000"); //Replace with your SID
    }
}
catch
{
    Console.WriteLine("Error deleting directory"); //Add exception detail messages...
}

Again, the above example would need you to identify the SID of the user before being able to send to the recycle bin.





io