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

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...