尝试从 Gzip 解压缩过程将文件流加载到 RichTextBox 时出现非静态错误
基本上我想要的只是将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是一种预感,但请尝试这样做:
Decompress.CopyTo(outFile)
是一个方法,不会返回任何内容,这可能就是 LoadFile 方法在该行上咳嗽的原因。另外,将您的函数更改为此(您不能在静态方法中引用您的控件):
Just a hunch, but try this instead:
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):
我最终所做的是一个 hack,但基本上我将未压缩的数据转储到一个文件中,然后将该文件加载到 RTF 中。我确信它比直接将其流式传输到 RTF 要慢得多,但我无法让该部分工作。它很实用,但不是很好。我根据程序参数将 fi 变量传递给 Decompress,然后指定该程序在用户双击 Windows 中的 gz 文件时运行。所以代码看起来像这样:
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: