2009-08-08 4 views

Répondre

1

Si vous voulez utiliser les appels Win32 vous devrez d'abord devez utiliser DllImport pour importer le noyau, la syntaxe est comme quelque chose comme ceci et vous devez le faire pour chaque méthode que vous voulez utiliser (c'est tout pseudo-code non testé qui ne décrit que le concept), l'exemple de code convertit votre chemins d'accès aux chemins UNC afin que vous puissiez avoir de longs chemins de fichiers:

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

Une fois que vous appelez FindFirstFile vous devez appeler FindNextFile pour le fichier suivant dans le répertoire, puis enfin FindClose; pour un exemple complet sur la façon de lister les fichiers dans un répertoire en utilisant l'apparence du noyau Win32 here

Questions connexes