繁体   English   中英

在c#中获取unicode目录名?

[英]get unicode directory name in c#?

我试图使用以下代码从我的USB驱动器获取所有目录

DirectoryInfo di = new DirectoryInfo("H:\\");
DirectoryInfo[] allDir = di.GetDirectories();

此代码适用于具有ascii名称的目录。 但是一个目录的名称为“”(unicode值U + 00A0)。 GetDirectories()无法获取该目录。 有没有办法获得目录与unicode名称?

好吧,你正在争取的是.NET框架使用FindFirstFile这个签名:

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false)]
internal static SafeFindHandle FindFirstFile(string fileName, [In, Out] Win32Native.WIN32_FIND_DATA data);

然后再加上对伤害的侮辱,你正在与语言作斗争:

自动为目标操作系统封送字符串。 默认值为Windows NT,Windows 2000,Windows XP和Windows Server 2003家族上的Unicode; 在Windows 98和Windows Me上默认为Ansi。 虽然公共语言运行库默认值为Auto,但语言可能会覆盖此默认值。 例如,默认情况下,C#将所有方法和类型标记为Ansi。

(从CharSet枚举文档重点补充

手头的问题是DllImport上的CharSet参数。 这意味着你只剩下一种方法; 自己利用P / Invoke。 你需要做很多事情。 首先,您需要返回的数据结构:

[BestFitMapping(false)]
[Serializable]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal class WIN32_FIND_DATA
{
  internal int dwFileAttributes;
  internal uint ftCreationTime_dwLowDateTime;
  internal uint ftCreationTime_dwHighDateTime;
  internal uint ftLastAccessTime_dwLowDateTime;
  internal uint ftLastAccessTime_dwHighDateTime;
  internal uint ftLastWriteTime_dwLowDateTime;
  internal uint ftLastWriteTime_dwHighDateTime;
  internal int nFileSizeHigh;
  internal int nFileSizeLow;
  internal int dwReserved0;
  internal int dwReserved1;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  internal string cFileName;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
  internal string cAlternateFileName;

  [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  public WIN32_FIND_DATA()
  {
  }
}

接下来,您将需要FindFirstFile的正确导入:

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = false)]
internal static SafeFindHandle FindFirstFile(string fileName, [In, Out] Win32Native.WIN32_FIND_DATA data);

接下来,您需要GetLastWin32Error来检查错误; 可以通过InteropServices名称空间中的Marshal.GetLastWin32Error访问。

接下来你需要迭代,所以你需要FindNextFile

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = false)]
internal static bool FindNextFile(SafeFindHandle hndFindFile, [MarshalAs(UnmanagedType.LPStruct), In, Out] Win32Native.WIN32_FIND_DATA lpFindFileData);

有了这些,您可以构建自己的迭代器。 但是,为了您的利益,这是Microsoft为“init”所做的事情:

[SecurityCritical]
private void CommonInit()
{
  string fileName = Path.InternalCombine(this.searchData.fullPath, this.searchCriteria);
  Win32Native.WIN32_FIND_DATA wiN32FindData = new Win32Native.WIN32_FIND_DATA();
  this._hnd = Win32Native.FindFirstFile(fileName, wiN32FindData);
  if (this._hnd.IsInvalid)
  {
    int lastWin32Error = Marshal.GetLastWin32Error();
    switch (lastWin32Error)
    {
      case 2:
      case 18:
        this.empty = this.searchData.searchOption == SearchOption.TopDirectoryOnly;
        break;
      default:
        this.HandleError(lastWin32Error, this.searchData.fullPath);
        break;
    }
  }
  if (this.searchData.searchOption == SearchOption.TopDirectoryOnly)
  {
    if (this.empty)
    {
      this._hnd.Dispose();
    }
    else
    {
      SearchResult searchResult = this.CreateSearchResult(this.searchData, wiN32FindData);
      if (!this._resultHandler.IsResultIncluded(searchResult))
        return;
      this.current = this._resultHandler.CreateObject(searchResult);
    }
  }
  else
  {
    this._hnd.Dispose();
    this.searchStack.Add(this.searchData);
  }
}

这就是他们为你所寻找的“迭代”做的事情:

  if (this.searchData != null && this._hnd != null)
  {
    while (Win32Native.FindNextFile(this._hnd, wiN32FindData))
    {
      SearchResult searchResult = this.CreateSearchResult(this.searchData, wiN32FindData);
      if (this._resultHandler.IsResultIncluded(searchResult))
      {
        if (this.needsParentPathDiscoveryDemand)
        {
          this.DoDemand(this.searchData.fullPath);
          this.needsParentPathDiscoveryDemand = false;
        }
        this.current = this._resultHandler.CreateObject(searchResult);
        return true;
      }
    }
    int lastWin32Error = Marshal.GetLastWin32Error();
    if (this._hnd != null)
      this._hnd.Dispose();
    if (lastWin32Error != 0 && lastWin32Error != 18 && lastWin32Error != 2)
      this.HandleError(lastWin32Error, this.searchData.fullPath);
  }

注意:您无法直接使用此代码,您必须将其与您的解决方案相匹配,但这是API的一个巨大的启动。 给你一份dotPeek来填补空白。

注意: FindFirstFile接受的fileName参数将转到父目录。 在你的情况下, H:\\

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM