2016-09-14 96 views
-1

我想使用OpenRead方法並運行到下面顯示的錯誤,我在代碼和參考中添加了using.system.IO,從下面的屏幕截圖中可以看到?我缺少什麼?如何獲取擺脫這個錯誤?缺少OpenRead方法的參考

using System; 
    using System.IO; 
    var zipFileName = @"C:\Temp\bins-9111.tar.bz2"; 

    using (FileStream fileToDecompressAsStream = zipFileName.OpenRead()) 
    { 
     string decompressedFileName = @"c:\bzip2\decompressed.txt"; 
     using (FileStream decompressedStream = File.Create(decompressedFileName)) 
     { 
      try 
      { 
       BZip2.Decompress(fileToDecompressAsStream, decompressedStream, true); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
      } 
     } 
    } 

錯誤: -

'string' does not contain a definition for 'OpenRead' and no extension method 'OpenRead' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) ConsoleApplication2 c:\users\gnakkala\documents\visual studio 2015\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs 32 

enter image description here

+6

ZipFileName是一個字符串 - 你想要的文件的實例。嘗試System.IO.File.OpenRead(pathToYourFile) –

+0

Shannon - 現在我得到'System'的同樣的錯誤....嘗試使用(FileStream fileToDecompressAsStream = zipFileName.System.IO.File.OpenRead())' – kemosabee

+0

如果zipFileName包含路徑和文件名,如c:\ myfiles \ myfile.zip,則使用File.OpenRead(zipFileName);否則,您必須將完整的URL傳遞到您的文件,如File.OpenRead(@「c:\ myfiles \」+ zipFileName); –

回答

2

您正在試圖調用字符串對象的有System.IO.File方法。 試試這個: 我改變了調用OpenRead來自文件對象而不是字符串。

var zipFileName = @"C:\Temp\bins-9111.tar.bz2"; 
using (System.IO.FileStream fs = System.IO.File.OpenRead(zipFileName)) 
{ 
     string decompressedFileName = @"c:\bzip2\decompressed.txt"; 
     using (FileStream decompressedStream = File.Create(decompressedFileName)) 
     { 
      try 
      { 
       BZip2.Decompress(fileToDecompressAsStream, decompressedStream, true); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
      } 
     } 
    } 
+0

Botanical-如何解壓縮在.bz2文件中存在的相同文件夾/文件結構中而不是文件中? – kemosabee

2

正如由@Shannon Holsinger在評論中提到OpenReadSystem.IO.File一個靜態方法,而不是字符串。

var zipFileName = @"C:\Temp\bins-9111.tar.bz2"; 
using (FileStream fileToDecompressAsStream = System.IO.File.OpenRead(zipFileName)) 
{/* rest of code */} 

有關更多信息,請參閱OpenRead文檔。

+0

嗨伊戈爾 - 我如何解壓在.bz2文件中存在的同一文件夾/文件結構而不是文件? – kemosabee

+1

@kemosabee - 在SO上使用新問題提出另一個問題。不要繼續通過評論提出新問題,或者完全改變現有問題。 – Igor

+0

如果此答案或任何其他人解決了您的問題,請將其標記爲已接受。不僅是對於這個問題,而且對於你將來會問的任何其他問題。 – Chichi