2017-06-29 33 views
0

我正在開發一個移動設備的閃存工具,它發送一個加載程序到它進一步 communications.currently我通過網絡請求從URL 接收加載器文件並將其存儲在磁盤中。下面是我使用在c中保存一個二進制文件到RAM#

private void submitData() 
    { 
     try 
     { 
      ASCIIEncoding encoding = new ASCIIEncoding(); 
      string cpuid = comboBox1.Text; 
      string postdata = "cpuid=" + cpuid; 
      byte[] data = encoding.GetBytes(postdata); 

      WebRequest request = WebRequest.Create("Http://127.0.0.1/fetch.php"); 
      request.Method = "POST"; 
      request.ContentType = "application/x-www-form-urlencoded"; 
      request.ContentLength = data.Length; 
      Stream stream = request.GetRequestStream(); 
      stream.Write(data, 0, data.Length); 
      stream.Close(); 

      WebResponse response = request.GetResponse(); 
      stream = response.GetResponseStream(); 

      StreamReader sr = new StreamReader(stream); 
      string path = sr.ReadToEnd(); 
      MessageBox.Show(path); 

      DateTime startTime = DateTime.UtcNow; 
      WebRequest request1 = WebRequest.Create("http://127.0.0.1/"+path); 
      WebResponse response2 = request1.GetResponse(); 
      using (Stream responseStream = response2.GetResponseStream()) 
      { 
       using (Stream fileStream = File.OpenWrite(@"e:\loader.mbn")) 
       { 
        byte[] buffer = new byte[4096]; 
        int bytesRead = responseStream.Read(buffer, 0, 4096); 
        while (bytesRead > 0) 
        { 
         fileStream.Write(buffer, 0, bytesRead); 
         DateTime nowTime = DateTime.UtcNow; 
         if ((nowTime - startTime).TotalMinutes > 1) 
         { 
          throw new ApplicationException(
           "Download timed out"); 

         } 

         bytesRead = responseStream.Read(buffer, 0, 4096); 
        } 
        MessageBox.Show("COMPLETDED"); 
       } 
      } 

      sr.Close(); 
      stream.Close(); 
     } 
     catch(Exception ex) 
     { 
      MessageBox.Show("ERR :" + ex.Message); 
     } 

我的問題是存在的,直接將文件存儲到RAM ,然後用它從那裏 到目前爲止,我試圖用內存流沒有結果的方式的代碼。

+0

MemoryStream工作正常。一個簡單的'responseStream.CopyTo(myMemoryStream)'足以將輸入複製到內存流中。實際上,您可以使用* same *來保存到文件流。你嘗試了什麼?有什麼問題? –

+0

是保存工作正常,但我無法將文件發送到主程序,因爲它需要加載程序的路徑和路徑只能是一個字符串,要麼我必須使用其他輸入方法接受流作爲輸入或必須放棄想法 –

+0

如果你實際上沒有發佈代碼並解釋發生了什麼問題,這是不可能的。 「無法發送」是什麼意思?如果數據已經存在於MemoryStream中,爲什麼要詢問'store to RAM'?您是否忘記將流的位置重置爲0? –

回答

0

MemoryStream沒有問題。如果你想使用數據,你必須記得重新設置流的位置。

//If the size is known, use it to avoid reallocations 
use(var memStream=new MemoryStream(response.ContentLength)) 
use(var responseStream=response.GetResponseStream()) 
{ 
    //Avoid blocking while waiting for the copy with CopyToAsync instead of CopyTo 
    await responseStream.CopyToAsync(memStream); 
    //Reset the position 
    memStream.Position = 0; 
    //Use the memory stream 
    ... 
} 
+0

'私人無效loadkernel) \t \t { \t \t stream.Position = 0; \t \t \t var sr = new Streamreader(strm); \t \t \t srloader = sr.ReadToeND(); \t \t} \t File.ReadAllBytes(srloader); }' –

+0

你在哪裏* rtite *到流?不要在這裏發佈代碼,更新問題。閱讀評論中的代碼非常困難。 MemoryStream不是'破碎的anwyay –

+0

它拋出錯誤非法字符路徑 –

相關問題