在 C#/.NET 中使用 SaveFileDialog 生成“无法创建抽象类的实例”错误

发布于 2024-11-16 08:05:03 字数 1019 浏览 3 评论 0原文

因此,我一直在努力在 .NET 应用程序中创建“保存”按钮。根据我的研究,我似乎做的每件事都是正确的。我一直将这篇文章作为主要来源: http ://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx

以下是我的C#方法代码:

private void save2(object A_0, EventArgs A_1)
{
    Stream stream = new Stream();
    savefile2 = new SaveFileDialog();
    savefile2.InitialDirectory = @"C:\Program Files\Folder\";
    savefile2.Filter = "Text files (*.txt)|*.txt|Word files (*.doc)|*.doc";
    savefile2.FilterIndex = 1;
    savefile2.FileName = "*.txt";
    savefile2.Title = "Save Box Text";
    savefile2.OverwritePrompt = true;
    if (savefile2.ShowDialog() == DialogResult.OK)
    {
        stream = savefile2.OpenFile();
        if (stream != null)
        {
            stream.Close();
        }
    }
}

运行程序时,我单击“保存”按钮查看是否会打开一个对话框,它会产生以下错误:

“无法创建抽象类的实例。”

但是,我没有使用任何抽象类。所有类都位于 .NET 框架内。所以,我被困住了。任何帮助将不胜感激。

So I have been struggling to create a "Save" button in my .NET application. I seem to be doing everything correctly according to my research. I have been referring to this article as a main source: http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx

The following is my method code in C#:

private void save2(object A_0, EventArgs A_1)
{
    Stream stream = new Stream();
    savefile2 = new SaveFileDialog();
    savefile2.InitialDirectory = @"C:\Program Files\Folder\";
    savefile2.Filter = "Text files (*.txt)|*.txt|Word files (*.doc)|*.doc";
    savefile2.FilterIndex = 1;
    savefile2.FileName = "*.txt";
    savefile2.Title = "Save Box Text";
    savefile2.OverwritePrompt = true;
    if (savefile2.ShowDialog() == DialogResult.OK)
    {
        stream = savefile2.OpenFile();
        if (stream != null)
        {
            stream.Close();
        }
    }
}

When running the program, I click the "Save" button to see if it will open a dialog box, and it produces the following error:

"Instances of abstract classes cannot be created."

However, I am not using any abstract classes. All the classes are within the .NET framework. So, I'm stuck. Any help would be appreciated.

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

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

发布评论

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

评论(3

影子是时光的心 2024-11-23 08:05:03

正如前两张海报所提到的,您不能在 Stream 类上调用 new,因为它是抽象的,这里是代码的编译版本以供参考

    private SaveFileDialog savefile2;
    private void save2(object A_0, EventArgs A_1)
    {
        savefile2 = new SaveFileDialog
                        {
                            InitialDirectory = @"C:\Program Files\Folder\",
                            Filter = "Text files (*.txt)|*.txt|Word files (*.doc)|*.doc",
                            FilterIndex = 1,
                            FileName = "*.txt",
                            Title = "Save Box Text",
                            OverwritePrompt = true
                        };
        if (savefile2.ShowDialog() == DialogResult.OK)
        {
            using (FileStream stream = File.Open(savefile2.FileName, FileMode.OpenOrCreate))
            {
                //do stuff
            }
        }
    }

请注意,最好将流实例化包装在 using() {} 块中确保它们得到妥善处置

As was mentioned by the previous two posters you can't call new on the Stream class because it's abstract, here's a compiling version of your code for reference

    private SaveFileDialog savefile2;
    private void save2(object A_0, EventArgs A_1)
    {
        savefile2 = new SaveFileDialog
                        {
                            InitialDirectory = @"C:\Program Files\Folder\",
                            Filter = "Text files (*.txt)|*.txt|Word files (*.doc)|*.doc",
                            FilterIndex = 1,
                            FileName = "*.txt",
                            Title = "Save Box Text",
                            OverwritePrompt = true
                        };
        if (savefile2.ShowDialog() == DialogResult.OK)
        {
            using (FileStream stream = File.Open(savefile2.FileName, FileMode.OpenOrCreate))
            {
                //do stuff
            }
        }
    }

Note that it's a good idea to wrap your stream instantiations in a using() {} block to ensure that they are disposed properly

穿透光 2024-11-23 08:05:03

Stream 类是抽象的,您可以使用它正在尝试创建第 1 行中的实例。您实际上从未使用过在那里创建的对象,因此要么不在声明中为其分配任何内容,要么为其分配 null

The Stream class is abstract, which you're attempting to create an instance of in line 1. You're not actually ever using the object created there, so just either don't assign anything to it in the declaration, or assign null to it.

南冥有猫 2024-11-23 08:05:03

问题是 Stream 是一个抽象类,因此无法像使用 Stream stream = new Stream(); 那样构造 Stream stream = new Stream(); 将该行更改为 Stream stream;< /code> 它应该可以工作。

The problem is Stream is an abstract class and therefore cannot be constructed as you did with Stream stream = new Stream(); change that line to Stream stream; and it should work.

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