尝试从 Gzip 解压缩过程将文件流加载到 RichTextBox 时出现非静态错误

发布于 2024-12-15 06:56:42 字数 1153 浏览 0 评论 0原文

基本上我想要的只是将 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

Basically all I want is to load a Gziped file into a rich text box. I found some code on the MS .NET site for decompressing the file. Now I want to point that stream to a rich text box, but I keep getting the error "An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.richTextBox1' "

Code is here. What am I doing wrong? Thanks in advance.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

合久必婚 2024-12-22 06:56:42

只是一种预感,但请尝试这样做:

richTextBox1.LoadFile(outFile, RichTextBoxStreamType.PlainText);

Decompress.CopyTo(outFile) 是一个方法,不会返回任何内容,这可能就是 LoadFile 方法在该行上咳嗽的原因。

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

public void Decompress(FileInfo fi)

Just a hunch, but try this instead:

richTextBox1.LoadFile(outFile, RichTextBoxStreamType.PlainText);

The Decompress.CopyTo(outFile) is a method and doesn't return anything, which is probably why the LoadFile method is coughing on that line.

Also, change your function to this (you can't have your control referenced in a static method):

public void Decompress(FileInfo fi)
坐在坟头思考人生 2024-12-22 06:56:42

我最终所做的是一个 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

What I ended up doing is a hack, but basically I dump the uncompressed data to a file then load that file in the RTF. I'm sure it's much slower than streaming it directly to RTF but I could not get that piece working. It's functional, but not great. I pass in the fi variable to Decompress based on what the program arguement is, and I then assign that program to be run when a user double clicks on a gz file in windows. So the code looks like this:

   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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文