【问题标题】:FileSystemWatcher works 3-5 times before throwing exceptions - C#FileSystemWatcher 在抛出异常之前工作 3-5 次 - C#
【发布时间】:2016-11-01 20:50:20
【问题描述】:

文件以 .today 形式通过 EDI 传入。我需要将其更改为 .txt 并将其移动到另一个文件夹。对于大约 3-5 个文件,一切正常,然后开始抛出异常。我尝试处理这些,但这并不能解决问题。我也遇到了零星的(文件名不能为空)异常。我似乎无法弄清楚这一点。

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

【问题讨论】:

  • 有时它在抛出异常之前只工作一次,有时在抛出之前连续 5 或 6 次。很奇怪。
  • 发生在哪一行?
  • FileSystemWatcher 是出了名的难以使用,因为它有很多错误状态。如果您愿意使用 Rx 并且不介意其他履行公司创建的代码,我有 a wrapper(也在 NuGet)我创建了它以使其更加可靠。
  • 斯坦利,它跳来跳去。有时我会收到“异常捕获”消息,有时我会收到其他消息。当那些不抛出时,我得到“文件名不能为空”异常。顺便说一句,我正在使用 Topshelf。

标签: c# ioexception filesystemwatcher file-conversion argumentnullexception


【解决方案1】:

也许这些文件仍在使用中。创建文件时会引发 FileSystemWatcher 事件,但创建过程仍然可以写入文件。请参阅here 了解可能的解决方案。

【讨论】:

  • 感谢您的宝贵时间!我会继续研究
猜你喜欢
  • 1970-01-01
  • 2014-02-06
  • 2017-02-24
  • 1970-01-01
  • 2014-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多