Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

c# - IS it possible to have savefiledialog () in windows console applications

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);
                }

            }
        }
    }
}

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?)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to add a reference to the System.Windows.Forms assembly.

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();
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...