파일은 EDI를 통해 .today로 제공됩니다. .txt로 변경하고 다른 폴더로 이동해야합니다. 모든 것은 3-5 파일에 대해 잘 작동하고, 예외를 던지기 시작합니다. 나는 그것들을 다루려고했지만 그것으로는 문제가 해결되지 않는다. 나는 또한 간헐적으로 (파일 이름은 null 일 수 없다) 예외도 받고있다. 나는 이것을 알아낼 수 없다.FileSystemWatcher는 예외를 throw하기 전에 3-5 번 작동합니다. - C#
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;
}
}
}
때로는 예외를 throw하기 전에 한 번만 작동하며 때로는 던지기 전에 연속적으로 5 또는 6이됩니다. 엄청 이상해. – Derek
어떤 행이 발생합니까? –
'FileSystemWatcher'는 많은 에러 상태를 가지고 있기 때문에 악명 높게 사용하기가 어렵습니다. Rx를 사용할 수 있고 다른 이행 회사에서 작성한 코드는 신경 쓰지 않으면 [wrapper] (https://idcomlog.codeplex.com/SourceControl/latest#IdComLog.Reactive/FileSystem.cs)가 있습니다 (또한 [NuGet] (https://www.nuget.org/packages/IdComLog.Reactive/)에서 훨씬 더 신뢰할 수 있도록 만들었습니다. –