2014-01-12 32 views
1

我已經構建了一個應用程序和一個可以在彼此之間傳輸文件的服務,但是似乎我的代碼(應該讀取閃存驅動器和CD上的目錄)僅適用於閃存驅動器。如何打開一個目錄來流過它

foreach (RadTreeNode l_node in m_fileExplorer.SelectedNodes) { 

     string l_sSelectedPath = l_node.Name; 
     FileAttributes l_fileAttr = File.GetAttributes(l_sSelectedPath); 

     if (l_fileAttr == FileAttributes.Directory) { 
      foreach (string l_sFilename in Directory.GetFiles(l_sSelectedPath, "*.*", SearchOption.AllDirectories)) { 
       m_qUpload.Enqueue(
        new UploadDescriptor { 
         BatchDescription = m_tbDescription.Text, 
         BatchTimestamp = l_now, 
         BatchId = l_sBatchId, 
         Username = m_frmLogin.Username, 
         TargetUsername = l_sTargetUsername, 
         Filename = l_sFilename 
        } 
       ); 

       AddProgressRow(l_sFilename); 
       l_nFilesInBatch++; 
       ms_sem.Release(); 
      } 
     } 
    } 

當我試圖做同樣的光盤,我得到這個錯誤:

System.UnauthorizedAccessException: Access to the path 'D:\' is denied. 
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) 
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) 
    at System.IO.File.Open(String path, FileMode mode, FileAccess access) 
    at ActivePath.FileUploader.Frontend.UploadForm.UploadEx(Object sender, DoWorkEventArgs e) in c:\code\Customers\FIBI\PWFileUploader\PWFileUploaderApplication\UploadForm.cs:line 697 

似乎都使用相同的代碼,我不能這樣做,但我對如何做到這一點不知道。

+0

任何一個可以幫助我嗎?請。 –

+0

您正試圖打開一個名爲'D:\\'的文件。這將*總是*失敗並出現拒絕訪問錯誤。它是一個目錄,而不是文件。你的代碼如何進入這個泡菜是不可能猜到的。使用調試器。 –

回答

4

從堆棧跟蹤的外觀看,有些東西叫File.Open。我假設UploadEx是你寫的東西,而且你在控制中。

檢查以確保FileModeFileAccess值是正確的。


這應該沒問題,因爲FileMode.Open說,如果它存在的文件只能被打開,並FileAccess.Read將防止寫入文件:

File.Open(somePath, FileMode.Open, FileAccess.Read); 

這些恐怕不會,因爲如果該文件不存在,你的程序將嘗試創建它,這在光盤上不會很好。或者,如果您的代碼無意中修改了該文件,那也不起作用。

File.Open(somePath, FileMode.OpenOrCreate); 

File.Open(somePath, FileMode.Open, FileAccess.ReadWrite); 

從你的代碼看,你似乎沒有被做任何事情比讀取目錄中的所有文件的更多,但我不能看到所有的代碼,所以我只是把它扔了作爲可能的答案。


好吧,下面是應爲你工作的基礎上,那個帖子我的評論鏈接的解決方案。我沒有真正建立這個,所以可能有一個或兩個語法錯誤。如果它不起作用,給我留言。

你當前foreach循環:

foreach (RadTreeNode l_node in m_fileExplorer.SelectedNodes) 
    { 
     string l_sSelectedPath = l_node.Name; 

     FileAttributes l_fileAttr = File.GetAttributes(l_sSelectedPath); 

     if (l_fileAttr == FileAttributes.Directory) 
      DoWhatever(l_sSelectedPath); 
    } 

掃描每個目錄遞歸,忽略拋出異常的一個新方法:

private void DoWhatever(string path) 
{ 
    try 
    { 
     Directory.GetFiles(path) 
       .ToList() 
       .ForEach(l_sFilename => 
       { 
        m_qUpload.Enqueue(
         new UploadDescriptor { 
          BatchDescription = m_tbDescription.Text, 
          BatchTimestamp = l_now, 
          BatchId = l_sBatchId, 
          Username = m_frmLogin.Username, 
          TargetUsername = l_sTargetUsername, 
          Filename = l_sFilename 
         } 
        ); 

        AddProgressRow(l_sFilename); 
        l_nFilesInBatch++; 
        ms_sem.Release(); 
       }); 

     Directory.GetDirectories(path) 
       .ToList() 
       .ForEach(s => DoWhatever(s)); 
    } 
    catch (UnauthorizedAccessException ex) 
    { 
     // We're not authorized to access this directory. Who knows why. Ignore it. 
    } 
} 
+0

當我試圖從CD讀取,它告訴我,我不能beacuse我不能打開文件沒有acces, –

+0

是的,我看到了你得到的異常。當嘗試寫入'c:'驅動器時,經常會出現'UnauthorizedAccessException',因爲它們沒有管理員權限。我能想到的唯一理由就是如果你試圖寫入CD,那麼你會得到它的CD-ROM驅動器。 –

+0

您可能還想閱讀[此類似文章](http://stackoverflow.com/questions/1393178/unauthorizedaccessexception-cannot-resolve-directory-getfiles-failure)。事實證明,他們無法訪問磁盤上的某個目錄。您正在指定'SearchOption.AllDirectories' ...可能對於CD,您可以嘗試一次搜索一個目錄。 –

0

我試着擺弄你的問題。你確定你通過FileAccess.Read標誌到File.Open方法嗎?試圖在沒有此標誌的情況下打開文件時,我確實收到錯誤。但是當你使用正確的標誌時,這很好。 TBH最好的方式是,如果你要顯示UploadEx方法的代碼,那麼它就是一個問題。

Error occuring when not using FileAccess.Read

現在適當標誌

File opening correctly

祝你好運,希望這會爲你工作:-)

MZ

相關問題