如何在控制台应用程序中选择文件和目标目录。 [英] How to select file and destination directory in console application.?

查看:174
本文介绍了如何在控制台应用程序中选择文件和目标目录。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[STAThread]

static void Main(string [] args)

{

FolderBrowserDialog fbd = new FolderBrowserDialog();

// fbd.Filter =txt文件(* .txt)| * .txt |所有文件(*。*)| *。*;



if(fbd.ShowDialog()== DialogResult.OK)

{

Console.WriteLine(Hello);



foreach(Directory.GetFiles中的var路径(fbd.SelectedPath))

{

Console.WriteLine(请选择文件夹) ;

Console.WriteLine(路径); //完整路径

Console.WriteLine(System.IO.Path.GetFileName(path)); //文件名

Console.WriteLine(路径+路径);

Console.ReadLine();

}

}





}

}



我尝试过:



我已经尝试过此代码来选择文件,但文件会自动被选中,因为它只允许选择文件夹而不是文件。

[STAThread]
static void Main(string[] args)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
// fbd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

if (fbd.ShowDialog() == DialogResult.OK)
{
Console.WriteLine("Hello");

foreach (var path in Directory.GetFiles(fbd.SelectedPath))
{
Console.WriteLine("Please select Folder");
Console.WriteLine(path); // full path
Console.WriteLine(System.IO.Path.GetFileName(path)); // file name
Console.WriteLine("path"+ path);
Console.ReadLine();
}
}


}
}

What I have tried:

I've tried this code for selecting the file but the file is automatically getting selected, as in it allows only to choose the folder and not the file.

推荐答案

如果要访问文件(仅限文件夹),请使用OpenFileDialog类:



OpenFileDialog类(System.Windows.Forms) [ ^ ]
Use the OpenFileDialog class if you want to access a file (vs only a folder):

OpenFileDialog Class (System.Windows.Forms)[^]


这篇关于如何在控制台应用程序中选择文件和目标目录。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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