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/