c# - 是否可以在 Windows 控制台应用程序中使用 savefiledialog()

标签 c# winforms console-application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Diagnostics
{
    class Program
    {
        static void Main(string[] args)
        {
            string filename = null;
            using (SaveFileDialog sFile = new SaveFileDialog())
            {
                sFile.Filter = "Text (Tab delimited)(*.txt)|*.txt|CSV (Comma separated)(*.csv)|*.csv";
                if (sFile.ShowDialog() == DialogResult.OK)
                {
                    filename = sFile.FileName;
                    WriteRegKey(diagnostic, filename);
                }

            }
        }
    }
}

我收到一个错误: 找不到类型或命名空间名称“SaveFileDialog”(是否缺少 using 指令或程序集引用?)

我确实尝试添加 System.Windows.Forms 命名空间,但我做不到。

最佳答案

您必须添加对 System.Windows.Forms 程序集的引用。

此外,您必须将 STAThread 属性添加到您的应用程序入口点方法。

[STAThread]
private static void Main(string[] args)
{
    using (SaveFileDialog sFile = new SaveFileDialog())
    {
        sFile.ShowDialog();
    }

    Console.ReadKey();
}

但老实说,这是一个糟糕的想法。控制台应用程序不应该有控制台本身的任何其他 UI。正如 SaveFileDialog 的命名空间所暗示的那样,SaveFileDialog 应该仅用于 Forms

关于c# - 是否可以在 Windows 控制台应用程序中使用 savefiledialog(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9620436/

相关文章:

c# - 如何摆脱枢轴控制中的标题? WIN10

c# - 有效使用正则表达式处理文件名的好方法是什么?

c++ - Xcode __pthread_kill + 20

nant - 如何在 TeamCity 中为控制台应用程序自动化单元测试?

c# - 如何按 2 列对 datagridview 进行排序

c# - 我需要在父级而不是子级中 InsertBefore 或 InsertAfter

c# - 改变WinForm中标题栏的颜色

vb.net - .Net FormStartPosition.CenterScreen 不居中

c# - 向datagridview c#添加新的空白行

c++ - C++ 编程语言 12.7 问题 2