我想在打印后移动文件。我正在使用FileSystemWatcher
。
private void Do_Automatic()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = Properties.Settings.Default.Chemin;
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = Properties.Settings.Default.Type_Fichier;
// Add event handlers.
//watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
//Console.WriteLine("Press \'q\' to quit the sample.");
//while (Console.Read() != 'q') ;
}
// Define the event handlers.
private void OnChanged(object source, FileSystemEventArgs e)
{
//MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);
DoSaveNPrint();
}
private void DoSaveNPrint()
{
try
{
string CheminBoite = Properties.Settings.Default.Chemin;
if (String.IsNullOrEmpty(CheminBoite))
{
MessageBox.Show("Chémin boite est vide");
}
else
{
DirectoryInfo di = new DirectoryInfo(CheminBoite);
FileInfo[] rgFiles = di.GetFiles();
foreach (FileInfo fi in rgFiles)
{
switch (Properties.Settings.Default.Type_Donne)
{
case "35-SOCAH":
Outils.Put_CSVtoSQL_SOCAH(CheminBoite + "\\" + fi.Name, Id);
break;
case "50-LISM":
Outils.Put_CSVtoSQL_LISM(CheminBoite + "\\" + fi.Name, Id);
break;
default:
Outils.Put_CSVtoSQL(CheminBoite + "\\" + fi.Name, Id);
break;
}
Dictionary<string, int> oDict = Outils.GetDataOrdre();
Outils.Print_BonEticket(true, Outils.Get_RightPrinter(Properties.Settings.Default.Zebra), oDict["NO_ORDRE"], oDict["NBR_COLIS"]);
string SourceFile = CheminBoite + "\\" + fi.Name;
string DestiFile = @"C:\Soft8_Donne\Histo\" + DateTime.Now.ToString("dd_MM_yyyy__hh_mm_ss___") + fi.Name;
WaitForFile(SourceFile, DestiFile);
}
GetData();
Outils.Insrt_Journax(sql_InstJounaux, Outils.Get_ComputerName().Trim(), "GetDataNPrint", 3, "3", "Reussi");
if (!Properties.Settings.Default.Is_Auto)
{
MessageBox.Show("La sauvegarde a été effectuée");
}
}
}
catch (Exception excThrown)
{
MessageBox.Show(excThrown.Message + Environment.NewLine + "Veuillez prevenir France alliance ");
Outils.Insrt_Journax(sql_InstJounaux, Outils.Get_ComputerName().Trim(), "problème GetDataNPrint", 3, "3", excThrown.Message);
}
}
/// <summary>
/// Blocks until the file is not locked any more.
/// </summary>
/// <param name="fullPath"></param>
bool WaitForFile(string fullPathSource, string fullPathDesti)
{
int numTries = 0;
while (true)
{
++numTries;
try
{
File.Move(fullPathSource, fullPathDesti);
return true;
}
catch (Exception ex)
{
MessageBox.Show(String.Format("WaitForFile {0} failed to get an exclusive lock: {1}", fullPathSource, ex.ToString()));
if (numTries > 10)
{
MessageBox.Show(String.Format("WaitForFile {0} giving up after 10 tries", fullPathSource));
return false;
}
// Wait for the lock to be released
System.Threading.Thread.Sleep(500);
}
}
}
使用我当前的代码,我收到错误,因为该文件正由另一个进程使用。