Come posso ottenere il nome localizzato di una cartella Windows "speciale" (Cestino, ecc.)?

StackOverflow https://stackoverflow.com/questions/37053

Domanda

Sto cercando di trovare l'API di Windows "corretta" per scoprire il nome localizzato delle cartelle "speciali", in particolare il Cestino.Voglio essere in grado di chiedere all'utente con una finestra di dialogo opportunamente localizzata chiedendogli se desidera inviare i file nel cestino o eliminarli direttamente.

Ho trovato molto su Internet (e su Stackoverflow) su come eseguire l'eliminazione vera e propria e sembra abbastanza semplice, voglio solo riuscire a localizzare il testo.

È stato utile?

Soluzione

Leggi questo articolo per esempi di codice e utilizzo:

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

Inoltre c'è un articolo su MSDN che ti aiuta Identificare la posizione delle cartelle speciali con chiamate API

Altri suggerimenti

In realtà non ho trovato l'articolo di CodeProject molto utile, quindi ho pensato di rispondere a questa domanda con il codice effettivo che ho utilizzato per recuperare il nome localizzato del cestino.

Questo esempio tenta inoltre di comportarsi correttamente per quanto riguarda la liberazione delle risorse.Qualsiasi commento è benvenuto, soprattutto se noti un errore nella gestione delle mie risorse!

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}");
   }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top