简体   繁体   English

FileSystemWatcher在抛出异常之前工作3-5次-C#

[英]FileSystemWatcher works 3-5 times before throwing exceptions - C#

File comes in over EDI as .today. 文件今天以EDI形式通过EDI传入。 I need to change it to .txt and move it to another folder. 我需要将其更改为.txt并将其移动到另一个文件夹。 Everything works just fine for about 3-5 files, then starts throwing exceptions. 对于大约3-5个文件,一切正常,然后开始引发异常。 I tried handling those, but that doesn't solve the problem. 我尝试处理这些问题,但这不能解决问题。 I'm also getting sporadic (filename cannot be null) exceptions as well. 我也有零星(文件名不能为null)异常。 I just can't seem to figure this out. 我似乎无法弄清楚。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Security.Permissions;

namespace FileConverterService
{


    class ConverterService
    {



        //Configure watcher & input
        private FileSystemWatcher _watcher;

        public bool Start()
        {
            _watcher = new FileSystemWatcher(@"C:\FTP_base\temp", "*.today");

            _watcher.Created += new FileSystemEventHandler(FileCreated);

            _watcher.IncludeSubdirectories = false;

            _watcher.EnableRaisingEvents = true;

            return true;
        }




        //Configure output creation and append file name to include .txt extension
        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        private void FileCreated(object sender, FileSystemEventArgs e)
        {
            try
            {
                string dateTime = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                string content = File.ReadAllText(e.FullPath);
                string upperContent = content.ToUpperInvariant();
                var dir = Path.GetDirectoryName(e.FullPath);
                var convertedFileName = Path.GetFileName(e.FullPath) + dateTime + ".txt";
                var convertedPath = Path.Combine(dir, convertedFileName);

                File.WriteAllText(convertedPath, upperContent);


            }
            catch (IOException f)
            {
                if (f is IOException)
                {
                    MessageBox.Show("Exception Caught"); //was just testing
                }
            }

            MoveConvert();
        }

        //Move converted file to EDI processing folder
        public static void MoveConvert()
        {
            try { 
            string dateTime = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            string rootFolderPath = @"C:\FTP_base\temp\";
            string moveTo = @"C:\FTP_base\INbound\inbound_" + dateTime + ".txt";
            //string moveTo = @"F:\FTP_base\Office Depot\INbound\inbound_" + dateTime + ".txt";
            string filesToMove = @"*.txt";   // Only move .txt

            string myfile2 = System.IO.Directory.GetFiles(rootFolderPath, filesToMove).FirstOrDefault();
            string fileToMove = myfile2;

            //moving file
            File.Move(fileToMove, moveTo);


                MoveOriginal();

            }
            catch (IOException e)
            {
                if (e is IOException)
                {
                    MessageBox.Show("File already exists."); //was just testing
                }
            }
        }


        public static void MoveOriginal()
        {
            try { 
            string dateTime = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            string rootFolderPath2 = @"C:\FTP_base\temp\";
            string moveTo2 = @"C:\FTP_base\archive\archive_" + dateTime + ".archive";
            //string moveTo2 = @"F:\Xcelerator_EDI\OfficeDepot\DataFiles\Inbound\Archive2\archive_" + dateTime + ".archive";
            string filesToMove2 = @"*.today";   // Only move .today


            string myfile = System.IO.Directory.GetFiles(rootFolderPath2, filesToMove2).FirstOrDefault();
            //foreach (string file in fileList)

            string fileToMove2 = myfile;

            //moving file
            File.Move(fileToMove2, moveTo2);

            }
            catch (IOException e)
            {
                if (e is IOException)
                {
                    MessageBox.Show("IO Exception Occurred"); //was just testing
                }
            }
        }



        //Stop Service control
        public bool Stop()
        {
            _watcher.Dispose();

            return true;
        }
    }
}

Perhaps the files are still in use. 也许文件仍在使用中。 The FileSystemWatcher event is raised when a file is created, but the creating process can still be writing to it. 创建文件时会引发FileSystemWatcher事件,但是创建过程仍然可以对其进行写入。 See here for a possible solution. 请参阅此处以了解可能的解决方案。

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

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