C# 如何在控制台应用程序c中使用ShowFileDialog#
如何使用C# 如何在控制台应用程序c中使用ShowFileDialog#,c#,C#,如何使用ShowFileDialog()将filepath分配给字符串file123 List lines=File.ReadAllLines(file123.ToList(); foreach(行中的字符串行) { 控制台写入线(行); } 您需要什么 第一加 using System.Windows.Forms; 到您的控制台项目 那你需要做标记 [STAThread] 在你的主要方法之上 并测试了下面的代码 [STAThread] static void
ShowFileDialog()将filepath分配给字符串file123
代码>
List lines=File.ReadAllLines(file123.ToList();
foreach(行中的字符串行)
{
控制台写入线(行);
}
您需要什么
第一加
using System.Windows.Forms;
到您的控制台项目
那你需要做标记
[STAThread]
在你的主要方法之上
并测试了下面的代码
[STAThread]
static void Main(string[] args)
{
string xyz = string.Empty;
OpenFileDialog openFileDialog = new OpenFileDialog();
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK)
xyz = openFileDialog.FileName;
Console.WriteLine(xyz);
Console.ReadKey();
}
首先,您必须向System.Windows.Forms.dll添加一个引用才能使其正常工作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; // Required to use OpenFileDialog.
using System.IO; // Required to read/write to files.
namespace ConsoleApp1
{
class Program
{
[STAThread] // This attribute is required to access OLE related functions.
static void Main(string[] args)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Multiselect = false;
string file123 = "";
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{ file123 = openFileDialog1.FileName; }
List<string> lines = File.ReadAllLines(file123).ToList();
foreach (string line in lines)
{ Console.WriteLine(line); }
Console.ReadKey();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;//需要使用OpenFileDialog。
使用System.IO;//需要读取/写入文件。
名称空间控制台EAPP1
{
班级计划
{
[StatThread]//访问OLE相关函数需要此属性。
静态void Main(字符串[]参数)
{
OpenFileDialog openFileDialog1=新建OpenFileDialog();
openFileDialog1.Multiselect=false;
字符串file123=“”;
if(openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{file123=openFileDialog1.FileName;}
List lines=File.ReadAllLines(file123.ToList();
foreach(行中的字符串行)
{Console.WriteLine(行);}
Console.ReadKey();
}
}
}
欢迎来到StackOverflow。我们需要更多地了解file123是什么以及您希望它是什么。看见