1

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

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

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
4

2 回答 2

2

只是预感,但试试这个:

richTextBox1.LoadFile(outFile, RichTextBoxStreamType.PlainText);

Decompress.CopyTo(outFile)是一个方法并且不返回任何东西,这可能是 LoadFile 方法在该行咳嗽的原因。

另外,将您的函数更改为此(您不能在静态方法中引用您的控件):

public void Decompress(FileInfo fi)
于 2011-11-10T21:55:20.557 回答
0

我最终做的是 hack,但基本上我将未压缩的数据转储到一个文件中,然后将该文件加载到 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
于 2011-11-16T17:46:24.670 回答