浏览在控制台应用程序文件夹中 [英] browse for folder in Console Application

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

问题描述

我现在有代码,允许我读的所有文件夹的文件,并将其写入到控制台。下面,我也已经得到了代码从使用浏览器目录中选择单个文件。我想知道我将如何能够选择使用浏览按钮的文件夹。



代码检查所有文件



 的foreach(在Directory.GetFiles(VAR路径@C:\Name\Folder\))
$ { b $ b Console.WriteLine(路径); //完整路径
Console.WriteLine(System.IO.Path.GetFileName(路径)); //文件名
}



代码以打开对话框

 打开文件对话框fileSelectPopUp =新的OpenFileDialog(); 
fileSelectPopUp.Title =;
fileSelectPopUp.InitialDirectory = @C:\;
fileSelectPopUp.Filter =所有的Excel文件(* .XLSX *)| *。*的.xlsx |所有文件(*。*)| *。*;
fileSelectPopUp.FilterIndex = 2;
fileSelectPopUp.RestoreDirectory = TRUE;
如果(fileSelectPopUp.ShowDialog()== DialogResult.OK)
{
textBox1.Text = fileSelectPopUp.FileName;
}


解决方案

首先,你需要添加引用到 System.Windows.Forms的



然后,添加 STAThread 属性的主要方法。这表明你的程序是单线程的,并使它与COM组件(该系统对话框使用)工作。



这之后,才可以使用的FolderBrowserDialog

 静态类节目
{$ b $> b〔STAThread]
静态无效的主要(字串[] args)
{
的FolderBrowserDialog FBD =新的FolderBrowserDialog();
如果(fbd.ShowDialog()== DialogResult.OK)
{
控制台
{
的foreach(在Directory.GetFiles(fbd.SelectedPath)VAR路径) .WriteLine(路径); //完整路径
Console.WriteLine(System.IO.Path.GetFileName(路径)); //文件名
}
}


}
}


I currently have to code to allow me to read all of the files of a folder and write them to the console. Below, I also have got the code to select individual files from a directory using a browser. I would like to know how I would be able to select a folder using a browse button.

code to check all files

  foreach(var path in Directory.GetFiles(@"C:\Name\Folder\"))
    {
       Console.WriteLine(path); // full path
       Console.WriteLine(System.IO.Path.GetFileName(path)); // file name
    }

Code to open dialog box

OpenFileDialog fileSelectPopUp = new OpenFileDialog();
            fileSelectPopUp.Title = "";
            fileSelectPopUp.InitialDirectory = @"c:\";
            fileSelectPopUp.Filter = "All EXCEL FILES (*.xlsx*)|*.xlsx*|All files (*.*)|*.*";
            fileSelectPopUp.FilterIndex = 2;
            fileSelectPopUp.RestoreDirectory = true;
            if (fileSelectPopUp.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = fileSelectPopUp.FileName;
            }

解决方案

First you need to add reference to System.Windows.Forms

Then, Add STAThread Attribute to the main method. This indicates that your program is single-threaded and enabled it to work with COM components (which the System dialogs use).

After that only you can use the FolderBrowserDialog with the Console Application

static class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        if (fbd.ShowDialog() == DialogResult.OK)
        {
            foreach (var path in Directory.GetFiles(fbd.SelectedPath))
            {
                Console.WriteLine(path); // full path
                Console.WriteLine(System.IO.Path.GetFileName(path)); // file name
            }
        }


    }
}

这篇关于浏览在控制台应用程序文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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