2015-04-08 35 views
0

我正在使用Visual C#Express 10.我試圖從另一個.avi文件創建,打開並寫入.avi文件。我爲y項目使用了Aforge dlls。下面的代碼是通過點擊按鈕編程的。訪問路徑在C#Visual Express 2010中被拒絕

OpenFileDialog toy = new OpenFileDialog(); 

      toy.Filter = "AVI files (*.avi)|*.avi|All files (*.*)|*.*"; 
      toy.Title = "Open an AVI file"; 
      toy.ShowDialog(); 
      string wish = toy.FileName; 
      VideoFileReader reader = new VideoFileReader(); 
      VideoFileWriter writer = new VideoFileWriter(); 
      string fileName = @"C:\test.avi"; 
      FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate); 

我收到提示在上面一行

  // open video file 
      reader.Open(wish); 
      for (int i = 0; i < 100; i++) 
      { 
       Bitmap videoFrame = reader.ReadVideoFrame(); 
       int w = videoFrame.Width; 
       int h = videoFrame.Height; 
       writer.Open(@"C:\test.avi",w, h); 
       videoFrame.Save(i + ".bmp"); 
       // dispose the frame when it is no longer required 
       //videoFrame.Dispose(); 
      } 
      reader.Close(); 

我收到的錯誤是 「進入 'C:\ test.avi' 的路徑被拒絕」。 我已經在StackOverflow中嘗試了一些解決方案,其中包括更改C驅動器的權限和使用環境文件夾。請告訴我這個。編輯: 對不起,延遲。我也不能寫日誌文件。我試圖上傳圖片

編輯2: 這是一個例外。我從文本框中得到它,然後輸入它。我用xxxx取代了我的名字。

System.UnauthorizedAccessException: Access to the path 'C:\test.avi' 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, Int32 bufferSize, FileOptions options, String msgPath, Boolean 
bFromProxy) 
at System.IO.FileStream..ctor(String path, FileMode mode) 
at VideoFramesTest.Form1.OpenAndSave() in 
C:\Users\XXXX\documents\visual studio 
2010\projects\VideoFramestest\VideoFramestest\Form1.cs:line42 
+0

好吧,看起來Aforge與此無關 - 只需創建「FileStream」,您的整個問題就可以在沒有任何對話或類似的情況下編寫。畢竟,你不會寫任何文件。這是什麼環境 - 它是一個本地客戶端應用程序,還是一個Web應用程序,或者是什麼?你能夠從命令行或在資源管理器中寫入驅動器的根目錄嗎? –

+0

它是一個窗體。它是大型應用程序的第一步。我需要包含幾個功能,但我一開始就陷入困境。至於你的其他問題,我可以通過命令行寫入根文件夾。我不完全知道如何使用瀏覽器編寫。 – DetectiveFishLegs

+0

這可能是各種不同的事情,將你的'writer.open'封裝在try catch中併發布異常。文件可能會打開,您可能需要管理員權限。我們不知道 – user1

回答

0

好的。我這樣做解決了我的問題。 我將保存文件路徑更改爲c:users .... desktop 顯然,我們不能直接在C:中編寫。 它也適用於在C中創建一個新文件夾:(如C:\ test \ test.avi) 那就是解決方案。 而且,我試過System.IO.File.Create(fileName)。也適用。 祝你好運。

0

我的猜測給出用C#你的經驗將是你有test.avi開放

更好的事情會來包裝你在一個try/catch像這樣的循環:

for (int i = 0; i < 100; i++) 
{ 
    try 
    { 

     Bitmap videoFrame = reader.ReadVideoFrame(); 
     int w = videoFrame.Width; 
     int h = videoFrame.Height; 
     writer.Open(@"C:\test.avi",w, h); 
     videoFrame.Save(i + ".bmp"); 
     // dispose the frame when it is no longer required 
     //videoFrame.Dispose(); 
    } 
    catch (Exception ex) 
    { 
    //show my exception 
    } 
} 

我也建議你也看看Using Statements來處理資源

+0

它沒有test.avi被打開。此外,錯誤在於宣佈Streamwriter。 – DetectiveFishLegs

+0

@detectiveFishLegs,等等...你正試圖爲一個不存在的文件打開一個'FileStream'?你的問題就在那裏......你如何期待這種事情發生?你想回來什麼?確保你的文件存在之前新的FileStream只是常識 – user1

+0

'FileStream FS =新的FileStream(文件名,FileMode.OpenOrCreate);'這行應該可能是'FileStream FS =新的FileStream(希望,FileMode.OpenOrCreate) ;'在你的代碼中。拿出'test.avi' – user1