2009-08-08 16 views

回答

1

如果你想使用Win32調用你的所有文件的列表將第一必須使用DllImport導入內核,語法就像這樣,你必須爲你想要使用的每種方法做這個(這是所有未經過測試的僞代碼,只描述了這個概念),代碼示例將你的路徑UNC路徑,所以你可以有很長的文件路徑:

using Microsoft.Win32.SafeHandles; 
    ... 
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] 
      static extern SafeHandleMinusOneIsInvalid FindFirstFileW(string lpFileName, IntPtr lpFindFileData); 

    ... 

      public String FindFirstFile(string filepath) 
      { 
       // If file path is disk file path then prepend it with \\?\ 
       // if file path is UNC prepend it with \\?\UNC\ and remove \\ prefix in unc path. 
       if (filepath.StartsWith(@"\\")) 
        filepath = @"\\?\UNC\" + filepath.Substring(2, filepath.Length - 2); 
       else 
        filepath = @"\\?\" + filepath; 
... 
       SafeHandleMinusOneIsInvalid ret = FindFirstFileW(filepath, lpFindFileData); 
... 
      } 

之後你打電話,你用FindFirstFile必須調用FindNextFile目錄中的下一個文件,然後終於FindClose;有關如何使用Win32內核查看目錄中的文件的完整示例here