简体   繁体   English

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

[英]how to use the browse folder dialog in console application

I created a program that monitors what happens to the files inside a directory. 我创建了一个程序来监视目录中的文件发生了什么。 For example if I add a new file, it shows the path and the name of the new file. 例如,如果我添加一个新文件,它将显示新文件的路径和名称。 Though I coded it in Windows Forms, I want to change it to a Console Application so that it shows the results on the console 虽然我用Windows窗体编码,但我想将其更改为控制台应用程序,以便在控制台上显示结果

My question is that how can I browse a folder using a console? 我的问题是如何使用控制台浏览文件夹? any ideas thanks in advance 任何想法提前感谢

The Windows Forms code is below: 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);

            }
         }

this is my whole code 这是我的全部代码

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)));

        }
    }

      }
    }

First, you need to add reference to System.Windows.Forms Second, add STAThread attribute to your method. 首先,您需要添加对System.Windows.Forms引用。其次,将STAThread属性添加到您的方法中。

For example: 例如:

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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM