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 venerdì 5 gennaio 2007 18:08 | Filed Under [ C# ]

Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET