如何在控制台应用程序中使用浏览文件夹对话框

时间:2013-03-13 09:15:26

标签: c# console-application folderbrowserdialog

我创建了一个程序来监视目录中文件的内容。例如,如果我添加一个新文件,它会显示新文件的路径和名称。虽然我在Windows窗体中对其进行了编码,但我想将其更改为控制台应用程序,以便在控制台上显示结果

我的问题是如何使用控制台浏览文件夹? 任何想法提前感谢

Windows窗体代码如下:

 private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK) 
            {
                button1.Enabled = false;
                button2.Enabled = true;
                directoryPath = folderBrowserDialog1.SelectedPath;

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {

                if (Directory.Exists(directoryPath))
                {
                    textbox_append("monitor opened");
                    filesList = Directory.GetFiles(directoryPath);
                   timer1.Start();

                }
                else
                {
                    MessageBox.Show("folder does not exist.");
                }
            }
            catch (Exception ex)
            {
                  MessageBox.Show("Error." + ex.Message);

            }
         }

这是我的整个代码

namespace emin_lab2_Csharp
{

    public partial class Form1 : Form
    {

        public string directoryPath;

        string[] filesList, filesListTmp;
        IFileOperation[] opList = {         new FileProcByExt("jpeg"),
                                            new FileProcByExt("jpg"),
                                            new FileProcByExt("doc"),
                                            new FileProcByExt("pdf"),
                                            new FileProcByExt("djvu"),
                                            new FileProcNameAfter20()
                                            };

        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK) 
            {
                button1.Enabled = false;
                button2.Enabled = true;
                directoryPath = folderBrowserDialog1.SelectedPath;

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {

                if (Directory.Exists(directoryPath))
                {
                    textbox_append("monitor opened");
                    filesList = Directory.GetFiles(directoryPath);
                   timer1.Start();

                }
                else
                {
                    MessageBox.Show("Такой папки нету.");
                }
            }
            catch (Exception ex)
            {
                  MessageBox.Show("Error." + ex.Message);

            }
         }

        private void timer1_Tick(object sender, EventArgs e)
        {
            filesListTmp = Directory.GetFiles(directoryPath);
            foreach (var elem in Enumerable.Except<string>(filesListTmp, filesList))
            {
                textbox_append(elem);

                foreach (var op in opList)
                {
                    if (op.Accept(elem)) { op.Process(elem); textbox_append(elem + " - action is performed on the file"); }

                }
            }
            filesList = filesListTmp;

        }

       public void textbox_append(string stroka)
        {
            textBox1.AppendText(stroka);
            textBox1.AppendText(Environment.NewLine);
        }


    interface IFileOperation
    {
        bool Accept(string fileName);
        void Process(string fileName);
    }

    class FileProcByExt : IFileOperation
    {
        string extName;
        string folderName;

        public FileProcByExt(string ext = "")
        {
            extName = ext;           
            folderName = extName.ToUpper();

        }


        public bool Accept(string fileName)
        {
            bool res = false;

            if (Path.GetExtension(fileName) == "." + extName) res = true;
            return res;
        }
        public void Process(string fileName)
        {
            Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(fileName),
                                                   folderName));
            File.Move(fileName,
                      Path.Combine(Path.GetDirectoryName(fileName),
                                   folderName,
                                   Path.GetFileName(fileName)));   

        }
    }

    class FileProcNameAfter20 : IFileOperation
    {

        public bool Accept(string fileName)
        {
            return Path.GetFileNameWithoutExtension(fileName).Length > 20;
        }

        public void Process(string fileName)
        {
            int cnt = Path.GetFileNameWithoutExtension(fileName).Length;
           File.Copy(fileName,
                       Path.Combine(Path.GetDirectoryName(fileName),
                                   "longname_" + cnt + Path.GetExtension(fileName)));

        }
    }

      }
    }

1 个答案:

答案 0 :(得分:3)

首先,您需要添加对System.Windows.Forms的引用 其次,为您的方法添加STAThread属性。

例如:

using System;
using System.Windows.Forms;

namespace ConsoleApplication1
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            Console.Write(ofd.FileName);
        }
    }
}