如何编写文件/目录观察器的代码 [英] How to write code for file/directory watcher

查看:83
本文介绍了如何编写文件/目录观察器的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在开发一个应用程序从给定目录中动态读取csv文件。之后读取该文件移动另一个文件夹。我编写代码来读取csv并移动到另一个folder.csv文件读取文件移动到另一个文件夹后完美。应用程序等待另一个csv file.once在路径中创建csv文件时自动获取该csv文件但我写的代码却没有自动获取csv文件。请帮助如何从路径中自动获取csv文件生成文件。我附上下面的代码请解决这个问题。

谢谢

问候,

raju



Hi,
I am Develop one application read csv files dynamically from given directory.after read that file move another folder.i am write code to read csv and move to another folder.csv file read perfect after file move to another folder.After application wait for another csv file.once create csv file in path automatically take that csv file but i am write code but not taken csv file automatically.Please help how to taken automatically csv file from path after generate file. i am attach my code below Please solve this problem.
thank you
regards,
raju

DialogResult result = folderBrowserDialog1.ShowDialog();

               if (result == DialogResult.OK)
               {
                   string abc = folderBrowserDialog1.SelectedPath;
                   DirectoryInfo selectedPath = new DirectoryInfo(abc);
                   textBox1.Text = abc;
                   //string[] fileList = System.IO.Directory.GetFiles(abc, "*.csv");
                   // using (System.IO.StreamReader sr = new System.IO.StreamReader("c:/Raj/abcd.txt"))
                   {
                       string[] fileList = System.IO.Directory.GetFiles(abc, "*.csv");
                       for (int i = 0; i <= fileList.Length - 1; i++)
                       {
                           string abcd = fileList[i];
                           string fn = new FileInfo(fileList[i]).Name;
                           StreamReader sr = new System.IO.StreamReader(abcd);
                           string currentLine;
                           while ((currentLine = sr.ReadLine()) != null)
                           {
                               MessageBox.Show(currentLine);
                               textBox2.Text = currentLine;
                           }
                           sr.Close();
                           string sourceFile = abcd;
                           string destinationFile = @"C:\Raj\log\" + fn;
                           // To move a file or folder to a new location:
                           System.IO.File.Move(sourceFile, destinationFile);
                           MessageBox.Show("file moved");
                           FileSystemWatcher watcher = new FileSystemWatcher(abc, "*.csv");

推荐答案

我解决了这个问题附加解决代码:



I am Solve this problem Attached solve code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace ReadCsv
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void info( )
        {

            string[] fileList = System.IO.Directory.GetFiles(textBox1.Text, "*.csv");
            for (int i = 0; i <= fileList.Length - 1; i++)
            {
                string abcd = fileList[i];
                string fn = new FileInfo(fileList[i]).Name;
                StreamReader sr = new System.IO.StreamReader(abcd);
                string currentLine;
                while ((currentLine = sr.ReadLine()) != null)
                {
                    MessageBox.Show(currentLine);
                    textBox2.Text = currentLine;
                }
                sr.Close();
                string sourceFile = abcd;
                string destinationFile = @"C:\Raj\log\" + fn;
                // To move a file or folder to a new location:
                System.IO.File.Move(sourceFile, destinationFile);
                MessageBox.Show("file moved");

                // string pqr = watcher.Path;

                //FileSystemWatcher watcher = new FileSystemWatcher();
                //filename = Path.GetFullPath(filename);
                /// watcher.Created += new FileSystemEventHandler(Onchanged);

                // watcher.WaitForChanged(
               
            }
        }
        public void xyz()
        {
            try
            {
                DialogResult result = folderBrowserDialog1.ShowDialog();

                if (result == DialogResult.OK)
                {
                    string abc = folderBrowserDialog1.SelectedPath;
                    DirectoryInfo selectedPath = new DirectoryInfo(abc);
                    textBox1.Text = abc;
                    //string[] fileList = System.IO.Directory.GetFiles(abc, "*.csv");
                    //using (System.IO.StreamReader sr = new System.IO.StreamReader("c:/Raj/abcd.txt"))
                    //{

                    info();
                    //}
                    FileSystemWatcher watcher = new FileSystemWatcher(textBox1.Text, "*.csv");
                    string wonder = watcher.Path;

                    while (wonder != null)
                    {
                           info();
                    }
                  
                    
              }
               
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);

            }
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            xyz();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
  
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}


这篇关于如何编写文件/目录观察器的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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