如何获取“特殊”Windows文件夹(回收站等)的本地化名称?

时间:2022-09-15 06:09:19

I'm trying to find out the 'correct' windows API for finding out the localized name of 'special' folders, specifically the Recycle Bin. I want to be able to prompt the user with a suitably localized dialog box asking them if they want to send files to the recycle bin or delete them directly.

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

I've found lots on the internet (and on *) about how to do the actual deletion, and it seems simple enough, I just really want to be able to have the text localized.

我在互联网(以及*)上发现了很多关于如何进行实际删除的内容,看起来很简单,我真的希望能够将文本本地化。

2 个解决方案

#1


2  

Read this article for code samples and usage:

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

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

Also there is an article on MSDN that helps you Identify the Location of Special Folders with API Calls

此外,MSDN上有一篇文章可帮助您识别具有API调用的特殊文件夹的位置

#2


0  

I actually didn't find the CodeProject article terribly helpful, so I thought I'd answer this question with the actual code that I used to retrieve the localized name of the recycle bin.

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

This sample also tries to behave correctly with regard to freeing resources. Any comments are welcome, especially if you spot an error with my resource management!

此示例还尝试在释放资源方面正常运行。欢迎任何评论,特别是如果您发现我的资源管理错误!

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}");
   }
}

#1


2  

Read this article for code samples and usage:

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

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

Also there is an article on MSDN that helps you Identify the Location of Special Folders with API Calls

此外,MSDN上有一篇文章可帮助您识别具有API调用的特殊文件夹的位置

#2


0  

I actually didn't find the CodeProject article terribly helpful, so I thought I'd answer this question with the actual code that I used to retrieve the localized name of the recycle bin.

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

This sample also tries to behave correctly with regard to freeing resources. Any comments are welcome, especially if you spot an error with my resource management!

此示例还尝试在释放资源方面正常运行。欢迎任何评论,特别是如果您发现我的资源管理错误!

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}");
   }
}