1

我创建了一个程序来监视目录中的文件发生了什么。例如,如果我添加一个新文件,它会显示新文件的路径和名称。尽管我在 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)));

        }
    }

      }
    }
4

1 回答 1

4

首先,您需要添加对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);
        }
    }
}
于 2013-03-13T09:22:06.267 回答