首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在抛出异常之前,FileSystemWatcher会工作3-5次- C#

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

Stack Overflow用户
提问于 2016-06-30 07:14:13
回答 1查看 286关注 0票数 0

文件以.today的形式通过电子数据交换进入。我需要将其更改为.txt并将其移动到另一个文件夹。对于大约3-5个文件,一切正常,然后开始抛出异常。我试着处理这些问题,但这并不能解决问题。我也遇到了零星的异常(文件名不能为空)。我就是想不通。

代码语言:javascript
复制
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;
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-06-30 07:50:53

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38111571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档