2011-09-06 81 views
-1

我已經編寫了一些代碼來模擬獲取份額驅動器大小的過程。驅動器來自文本文件。問題是路徑達到超過250的舞臺? charachter限制。無論如何,這是可以避免的。模擬獲取份額大小的過程

我發現上線,建議把東西@「\?\」文件路徑之前,但我unsurue它是否會爲definate工作,如果我正確或者不使用它?

感謝

 void getSizes_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
     { 
      String val = ""; 
      float megabytes = ((float)e.Result/1024f)/1024f; 
      if (megabytes > 10240) //greater than 10 gig 
       val = (megabytes/1024.0).ToString() + "GB"; 
      else 
       val = megabytes + "MB"; 
      textBox1.Text += val; 
      textBox1.Text += Environment.NewLine; 
     } 

     void getSizes_DoWork(object sender, DoWorkEventArgs e) 
     { 
      String s = (String)e.Argument; 
      String path = Path.GetFullPath(s); 
      float bytes = (float)GetDirectorySize((String)e.Argument); 
      e.Result = bytes; 
     } 

     protected static float GetDirectorySize(string folder) 
     { 
      float folderSize = 0.0f; 
      try 
      { 
       //Checks if the path is valid or not 
       if (!Directory.Exists(folder)) 
        return folderSize; 
       else 
       { 
        try 
        { 
         foreach (string file in Directory.GetFiles(folder)) 
         { 
          String path = @"\\?\" + file; 
          if (File.Exists(path)) 
          { 
           FileInfo finfo = new FileInfo(path); 
           folderSize += finfo.Length; 
          } 
         } 

         foreach (string dir in Directory.GetDirectories(folder)) 
          folderSize += GetDirectorySize(dir); 
        } 
        catch (NotSupportedException e) 
        { 
         Console.WriteLine("Unable to calculate folder size: {0}", e.Message); 
        } 
       } 
      } 
      catch (UnauthorizedAccessException e) 
      { 
       Console.WriteLine("Unable to calculate folder size: {0}", e.Message); 
      } 
      return folderSize; 
     } 
    } 
} 
+0

請編輯您的問題只提供相關的代碼。 –

回答