是否有可能在Windows控制台应用程序savefiledialog() [英] IS it possible to have savefiledialog () in windows console applications

查看:166
本文介绍了是否有可能在Windows控制台应用程序savefiledialog()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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指令或程序集引用)

I am getting an error: The type or namespace name 'SaveFileDialog' could not be found (are you missing a using directive or an assembly reference?)

我也尝试添加 System.Windows.Forms的命名空间,但我不能。

I did try adding the System.Windows.Forms namespace, but I was not able to.

推荐答案

您要添加到 System.Windows.Forms的引用组装。

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

Also, you must add the STAThread attribute to your application entry point method.

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

    Console.ReadKey();
}



不过说实话,这是一个可怕的想法。控制台应用程序不应该有任何其他的UI控制台本身。由于 SaveFileDialog 显示, SaveFileDialog 应该用于表格

But honestly, that's a terrible idea. A console application shouldn't have any other UI that the console itself. As the namespace of SaveFileDialog suggests, SaveFileDialog should be used for Forms only.

这篇关于是否有可能在Windows控制台应用程序savefiledialog()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆