我正在尝试找出“正确”的 Windows API,以查找“特殊”文件夹(特别是回收站)的本地化名称。我希望能够通过适当本地化的对话框提示用户,询问他们是否要将文件发送到回收站或直接删除它们。

我在互联网(以及 Stackoverflow)上找到了很多关于如何进行实际删除的信息,而且看起来很简单,我只是真的希望能够将文本本地化。

有帮助吗?

解决方案

阅读本文以获取代码示例和用法:

http://www.codeproject.com/KB/winsdk/SpecialFolders.aspx

MSDN 上还有一篇文章可以帮助您 通过 API 调用识别特殊文件夹的位置

其他提示

实际上,我没有发现 CodeProject 文章非常有帮助,所以我想我应该用我用来检索回收站本地化名称的实际代码来回答这个问题。

该示例还尝试在释放资源方面表现正确。欢迎提出任何意见,特别是如果您发现我的资源管理有错误!

public static string GetLocalizedRecycleBinName()
{
   IntPtr relative_pidl, parent_ptr, absolute_pidl;

   PInvoke.SHGetFolderLocation(IntPtr.Zero, PInvoke.CSIDL.BitBucket,
         IntPtr.Zero, 0, out absolute_pidl);
   try
   {
      PInvoke.SHBindToParent(absolute_pidl,
            ref PInvoke.Guids.IID_IShellFolder,
            out parent_ptr, out relative_pidl);
      PInvoke.IShellFolder shell_folder =
            Marshal.GetObjectForIUnknown(parent_ptr)
            as PInvoke.IShellFolder;
      // Release() for this object is called at finalization
      if (shell_folder == null)
         return Strings.RecycleBin;

      PInvoke.STRRET strret = new PInvoke.STRRET();
      StringBuilder sb = new StringBuilder(260);
      shell_folder.GetDisplayNameOf(relative_pidl, PInvoke.SHGNO.Normal,
            out strret);
      PInvoke.StrRetToBuf(ref strret, relative_pidl, sb, 260);
      string name = sb.ToString();

      return String.IsNullOrEmpty(name) ? Strings.RecycleBin : name;
   }
   finally { PInvoke.ILFree(absolute_pidl); }
}

static class PInvoke
{
   [DllImport("shell32.dll")]
   public static extern int SHGetFolderLocation(IntPtr hwndOwner,
         CSIDL nFolder, IntPtr hToken, uint dwReserved, out IntPtr ppidl);

   [DllImport("shell32.dll")]
   public static extern int SHBindToParent(IntPtr lpifq, [In] ref Guid riid,
         out IntPtr ppv, out IntPtr pidlLast);

   [DllImport("shlwapi.dll")]
   public static extern Int32 StrRetToBuf(ref STRRET pstr, IntPtr pidl,
         StringBuilder pszBuf, uint cchBuf);

   [DllImport("shell32.dll")]
   public static extern void ILFree([In] IntPtr pidl);

   [ComImport]
   [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
   [Guid("000214E6-0000-0000-C000-000000000046")]
   public interface IShellFolder
   {
      [PreserveSig]
      Int32 CompareIDs(Int32 lParam, IntPtr pidl1, IntPtr pidl2);
      void ParseDisplayName(IntPtr hwnd, IntPtr pbc, String pszDisplayName,
            UInt32 pchEaten, out IntPtr ppidl, UInt32 pdwAttributes);
      void EnumObjects(IntPtr hwnd, int grfFlags,
            out IntPtr ppenumIDList);
      void BindToObject(IntPtr pidl, IntPtr pbc, [In] ref Guid riid,
            out IntPtr ppv);
      void BindToStorage(IntPtr pidl, IntPtr pbc, [In] ref Guid riid,
            out IntPtr ppv);
      void CreateViewObject(IntPtr hwndOwner, [In] ref Guid riid,
            out IntPtr ppv);
      void GetAttributesOf(UInt32 cidl,
            [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)]
         IntPtr[] apidl, ref uint rgfInOut);
      void GetUIObjectOf(IntPtr hwndOwner, UInt32 cidl,
            [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]
         IntPtr[] apidl, [In] ref Guid riid, UInt32 rgfReserved,
         out IntPtr ppv);
      void GetDisplayNameOf(IntPtr pidl, SHGNO uFlags, out STRRET pName);
      void SetNameOf(IntPtr hwnd, IntPtr pidl, string pszName,
            int uFlags, out IntPtr ppidlOut);
   }

   public enum CSIDL
   {
      BitBucket = 0x000a,
   }

   public enum SHGNO
   {
      Normal = 0x0000, ForParsing = 0x8000,
   }

   [StructLayout(LayoutKind.Explicit, Size = 520)]
   public struct STRRETinternal
   {
      [FieldOffset(0)] public IntPtr pOleStr;
      [FieldOffset(0)] public IntPtr pStr;
      [FieldOffset(0)] public uint uOffset;
   }

   [StructLayout(LayoutKind.Sequential)]
   public struct STRRET
   {
      public uint uType;
      public STRRETinternal data;
   }

   public class Guids
   {
      public static Guid IID_IShellFolder =
            new Guid("{000214E6-0000-0000-C000-000000000046}");
   }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top