2013-05-04 121 views
-5

該計劃旨在設置文件路徑和想法是,當數據被設置,就應該使用這個功能:的FileStream拋出找不到文件

public void SendFile(String fileName, long fileSize, NetworkStream io) 
    { 
     SendFileNameToServer(); 
     SendFileSizeToServer(); 

     byte[] fileData; 

     try 
     { 
      if (!File.Exists(fileName)) 
      { 
       throw new FileNotFoundException("File does not exist!"); 
      } 
      FileStream openFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); 
      BinaryReader bReader = new BinaryReader(openFileStream); 

      Int32 remainingSize = Convert.ToInt32(_fileSize); 


      do 
      { 
       fileData = bReader.ReadBytes(BUFSIZE); 
       io.Write(fileData, 0, BUFSIZE); 
       remainingSize -= BUFSIZE; 
      } while (remainingSize > BUFSIZE); 

      do 
      { 
       fileData = bReader.ReadBytes(remainingSize); 
       io.Write(fileData, 0, remainingSize); 
       remainingSize -= remainingSize; 
      } while (remainingSize > 0); 

      bReader.Close(); 
      openFileStream.Flush(); 
      openFileStream.Close(); 
      io.Flush(); 
      io.Close(); 
     } 
     catch (Exception) 
     { 
      throw new Exception(); 
     } 
    } 

發送的文件路徑指定的文件到接收文件數據的服務器端程序。

問題是,當它到達行FileStream openFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);它告訴我該文件沒有找到。它給出的例外是Exception:Thrown: "The process cannot access the file 'D:\StepMania\Songs\Fragma\You Are Alive\Green.avi' because it is being used by another process." (System.IO.IOException) A System.IO.IOException was thrown: "The process cannot access the file 'D:\*FilePath*\Green.avi' because it is being used by another process." Time: 04-05-2013 21:11:39 Thread:Main Thread[5532],但我想不到在StepMania沒有運行時使用此文件的任何進程。 我知道該文件在那裏,因爲我檢查了文件路徑,它在那裏,因爲它應該是。它工作得很好,如果該文件是在程序完全相同的文件夾,但除此之外,我似乎無法找到解決這個問題。有沒有人有任何想法可能是錯的?

如果您需要更多我的代碼,請告訴我。

編輯: 我的服務器使用此代碼接收文件:

public void ReceiveFile(String fileName, NetworkStream io) 
    { 
     // TO DO Din egen kode 
     byte[] fileData = new byte[BUFSIZE]; 

     FileStream writeFileStream = new FileStream(fileName, FileMode.Create); 
     BinaryWriter bWrite = new BinaryWriter(writeFileStream); 

     int bytesRead = 0; 
     long remainingSize = Convert.ToInt32(_fileSize); 

     do 
     { 
      Console.WriteLine("Remaining number of bytes: {0}", remainingSize); 
      bytesRead = io.Read(fileData, 0, BUFSIZE); // Read max 1000 bytes from server via socket (actual value is placed in "bytesRead" 
      bWrite.Write(fileData, 0, bytesRead); // write the received bytes into file. the number of received bytes is placed in "bytesRead" 
      remainingSize -= bytesRead; 
     } 
     while (remainingSize > 0); 

     writeFileStream.Flush(); 
     writeFileStream.Close(); 
     bWrite.Close(); 
    } 

回答

0

好吧,我發現這個問題:我和我的客戶端程序interefered服務器端程序。下面是固定的代碼爲我的客戶端程序的代碼SENDFILE:

public void SendFile(String fileName, long fileSize, NetworkStream io) 
    { 
     SendFileNameToServer(); 
     SendFileSizeToServer(); 

     byte[] fileData; 

     try 
     { 
      FileStream openFileStream = File.OpenRead(fileName); 
      BinaryReader bReader = new BinaryReader(openFileStream); 

      Int32 remainingSize = Convert.ToInt32(_fileSize); 


      do 
      { 
       fileData = bReader.ReadBytes(BUFSIZE); 
       io.Write(fileData, 0, BUFSIZE); 
       remainingSize -= BUFSIZE; 
      } while (remainingSize > BUFSIZE); 

      do 
      { 
       fileData = bReader.ReadBytes(remainingSize); 
       io.Write(fileData, 0, remainingSize); 
       remainingSize -= remainingSize; 
      } while (remainingSize > 0); 

      openFileStream.Flush(); 
      bReader.Close(); 
      openFileStream.Close(); 
      io.Flush(); 
      io.Close(); 
     } 
     catch (Exception) 
     { 
      throw new Exception(); 
     } 
    } 

這裏的ReceiveFile代碼爲我的服務器:

public void ReceiveFile(String fileName, NetworkStream io) 
    { 
     // TO DO Din egen kode 
     byte[] fileData = new byte[BUFSIZE]; 

     FileStream writeFileStream = new FileStream(LIB.extractFileName(fileName), FileMode.Create); 
     BinaryWriter bWrite = new BinaryWriter(writeFileStream); 

     int bytesRead = 0; 
     long remainingSize = Convert.ToInt32(_fileSize); 

     do 
     { 
      Console.WriteLine("Remaining number of bytes: {0}", remainingSize); 
      bytesRead = io.Read(fileData, 0, BUFSIZE); // Read max 1000 bytes from server via socket (actual value is placed in "bytesRead" 
      bWrite.Write(fileData, 0, bytesRead); // write the received bytes into file. the number of received bytes is placed in "bytesRead" 
      remainingSize -= bytesRead; 
     } 
     while (remainingSize > 0); 

     writeFileStream.Flush(); 
     writeFileStream.Close(); 
     bWrite.Close(); 
    } 

再次謝謝大家誰回答我的帖子!

+0

不要忘記將此答案標記爲「已接受」。 – 2013-05-05 08:49:42

+0

@MatthewWatson它說,必須等到明天才能接受我自己的答案...... – user2350376 2013-05-05 14:05:35