이 당신이
가}
using System.Runtime.InteropServices;
class Win32ApiUtils
{
// Don't declare a value for the Pack size. If you omit it, the correct value is used when
// marshaling and a single SHFILEOPSTRUCT can be used for both 32-bit and 64-bit operation.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SHFILEOPSTRUCT
{
public IntPtr hwnd;
[MarshalAs(UnmanagedType.U4)]
public int wFunc;
[MarshalAs(UnmanagedType.LPWStr)]
public string pFrom;
[MarshalAs(UnmanagedType.LPWStr)]
public string pTo;
public ushort fFlags;
[MarshalAs(UnmanagedType.Bool)]
public bool fAnyOperationsAborted;
public IntPtr hNameMappings;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpszProgressTitle;
}
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
const int FO_DELETE = 3;
const int FOF_ALLOWUNDO = 0x40;
const int FOF_NOCONFIRMATION = 0x10; //Don't prompt the user.;
public static int DeleteFilesToRecycleBin(string filename)
{
SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
shf.wFunc = FO_DELETE;
shf.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
shf.pFrom = filename + "\0"; // <--- this "\0" is critical !!!!!
int result = SHFileOperation(ref shf);
// Any value different from zero is an error to lookup
return result;
}
프롬프트
없이 휴지통에있는 파일 및 디렉토리 이동에 필요한 것을 할 수있는 유일한 방법이 될 것으로 보인다
foreach (ListViewItem item in listView1.SelectedItems)
{
int result = Win32ApiUtils.DeleteFilesToRecycleBin(item.SubItems[3].Text);
if(result != 0) ...... // ??? throw ??? message to user and contine ???
}
- 경고 - 이 코드는 테스트해야합니다. PInvoke site에 SHFILEOPSTRUCT의 레이아웃을 찾았으며 해당 링크에 사용 된 문자열 선언에 대한 몇 가지 참고 사항이 있습니다.
음. 삭제할 단일 디렉토리가있는 Win7 64 비트에서 테스트되었습니다. 매력처럼 작동합니다 ....
[this] (http://www.daveamenta.com/2008-05/c-delete-a-file-to-the-recycle-bin/) (및 다른 많은 기사) 당신은 오직 그 옵션을 가지고 있습니다 SHFILEOPSTRUCT – Steve
답변으로 이것을 추가해야합니다 스티브, 아래의 "답변"은 모든 표를 얻고 있습니다. 당신의 도움을 주셔서 감사합니다. –
글쎄, 나는 SHFILEOPSTRUCT로 예제를 만들려고 시도하고 대답을 할 것이다. Wait – Steve