Around and About .NET World

Il blog di Marco Minerva
posts - 1671, comments - 2232, trackbacks - 2135

My Links

News

Contattami su Live Messenger:


MCTS: Windows, Web, Distributed Applications & SQL Server

MCPD: Enterprise Applications

Tag Cloud

Archives

Post Categories

Links

Spostare un file nel Cestino con C#

Utilizzando il metodo Delete degli oggetti File, FileInfo, Directory e DirectoryInfo, i file e le cartelle vengono cancellati dal sistema senza finire nel Cestino. Qualora si voglia passare per quest'ultimo, √® necessario fare ricorso al Platform Invoke:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct SHFILEOPSTRUCT { public IntPtr hwnd; public int wFunc; public string pFrom; public string pTo; public short fFlags; public bool fAnyOperationsAborted; public IntPtr hNameMappings; [MarshalAs(UnmanagedType.LPWStr)] public string lpszProgressTitle; } [DllImport("shell32.dll", CharSet = CharSet.Unicode)] private static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp); private const int FO_DELETE = 3; private const short FOF_ALLOWUNDO = 0x40; private const short FOF_NOCONFIRMATION = 0x10; public static void Delete(string path, bool showConfirmationDialog) { SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT(); shf.wFunc = FO_DELETE; if (showConfirmationDialog) shf.fFlags = FOF_ALLOWUNDO; else shf.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION; shf.pFrom = path + '\0'; SHFileOperation(ref shf); }

Questo codice, opportunamente arricchito, andrà a far parte del mio progetto Extension for .NET Framework, ma intanto ho voluto postarlo qui in anteprima per tutti gli affezionati lettori del mio blog smile_teeth.

Print | posted on Friday, January 5, 2007 4:08 PM | Filed Under [ C# ]

Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET