2011-11-10 27 views
1

基本上我只想将Gziped文件加载到富文本框中。我在MS .NET站点上找到了一些用于解压文件的代码。现在我想将该流指向一个富文本框,但我不断收到错误“非静态字段,方法或属性需要对象引用''WindowsFormsApplication1.Form1.richTextBox1''尝试从Gzip解压缩过程将文件流加载到RichTextBox时出现非静态错误

代码是这里。我究竟做错了什么?提前致谢。

public static void Decompress(FileInfo fi) 
{ 
    // Get the stream of the source file. 
    using (FileStream inFile = fi.OpenRead()) 
    { 
     // Get original file extension, for example 
     // "doc" from report.doc.gz. 
     string curFile = fi.FullName; 
     string origName = curFile.Remove(curFile.Length - 
       fi.Extension.Length); 

     //Create the decompressed file. 
     using (FileStream outFile = File.Create(origName)) 
     { 
      using (GZipStream Decompress = new GZipStream(inFile, 
        CompressionMode.Decompress)) 
      { 
       // Copy the decompression stream 
       // into the output file. 
       Decompress.CopyTo(outFile); 
       richTextBox1.LoadFile(Decompress.CopyTo(outFile), RichTextBoxStreamType.PlainText); 
       // problem right here ^^^^ 


      }//using 
     }//using 
    }//using 
}//DeCompress 

回答

2

只是一种预感,但试试这个来代替:

richTextBox1.LoadFile(outFile, RichTextBoxStreamType.PlainText); 

Decompress.CopyTo(outFile)是一种方法,不返回任何东西,这可能是为什么的LoadFile方法是在该行咳嗽。

而且,你的函数改变这个(你不能有你的控制在静态方法中引用):

public void Decompress(FileInfo fi) 
+0

你是对的。但是,这会产生与OP提到的不同的编译器错误。这没有意义。也许他只是贴错了一个。 –

+0

@Charles你是对的。他解除的例子是一种静态方法。 “这里的问题^^^^”指的是错误的问题。 – LarsTech

0

我最终什么事做的是一个黑客,但基本上我转储未压缩数据一个文件然后在RTF中加载该文件。我相信它比直接传输到RTF要慢很多,但我无法让这部分工作。它是功能性的,但不是很好。我将fi变量传递给Decompress,基于程序的参数是什么,然后在用户在Windows中双击gz文件时分配该程序以运行。所以代码如下所示:

public void Decompress(FileInfo fi) 
    { 
     // Get the stream of the source file. 
     using (FileStream inFile = fi.OpenRead()) 
     { 
      // Get original file extension, for example 
      // "doc" from report.doc.gz. 
      string curFile = fi.FullName; 
      string origName = curFile.Remove(curFile.Length - 
        fi.Extension.Length); 

      //Create the decompressed file. 
      using (FileStream outFile = File.Create(origName)) 
      { 
       using (GZipStream Decompress = new GZipStream(inFile, 
         CompressionMode.Decompress)) 
       { 
        // Copy the decompression stream 
        // into the output file. 
        Decompress.CopyTo(outFile); 
        Decompress.Close(); 
        outFile.Close(); 
        inFile.Close(); 
        rtbOut.LoadFile(origName, RichTextBoxStreamType.PlainText); 
        string tmp = rtbOut.Text; 
       }//using 
      }//using 
     }//using 
    } //Decompress 
相关问题