我有下面的代码,它使用计时器为我移动文件(每 10 分钟移动 200 个文件或我保存在我的设置表单上的文件)。
问题是文件在打开和写入时会被移动,我知道我必须在我的程序中“打开”它并检查异常但我不知道我应该把它放在我的代码中的哪个位置?
try
{
string origDir = @"" + Properties.Settings.Default.MoveFrom;
string destDir = @"" + Properties.Settings.Default.MoveTo;
string oldDir = @"" + Properties.Settings.Default.MoveToOld;
int filesPerMove = Properties.Settings.Default.FilesPerMove;
int i = 0;
DateTime dateMove= DateTime.Now;
DirectoryInfo dirInfo = new DirectoryInfo(origDir);
FileInfo[] files = dirInfo.GetFiles("*.txt");
foreach (string file in Directory.GetFiles(origDir))
if (new FileInfo(file).Length > 0)
{
if (i < filesPerMove)
{
ListViewItem lviSuccess = new ListViewItem(origFile.Name);
lviSuccess.SubItems.Add(origFile.LastWriteTime.ToString());
lviSuccess.SubItems.Add(dateMove.ToString());
lviSuccess.SubItems.Add(origFile.Length.ToString());
lviSuccess.SubItems.Add(origDir.ToString());
lviSuccess.SubItems.Add(destDir.ToString());
lvFileMoves.Items.Add(lviSuccess);
lviSuccess.UseItemStyleForSubItems = true;
lviSuccess.ForeColor = Color.Green;
FileInfo destFile = new FileInfo(file.Replace(origDir, destDir));
FileInfo destFile1 = new FileInfo(file.Replace(origDir, oldDir));
System.IO.File.Copy(origFile.FullName, destFile1.FullName, true);
System.IO.File.Copy(origFile.FullName, destFile.FullName, true);
File.Delete(origFile.FullName);
i++;
}
}
}
catch (System.IO.IOException ex)
{
ListViewItem lvi = new ListViewItem(ex.Message);
lvFileMoves.Items.Add(lvi);
lvi.UseItemStyleForSubItems = true;
lvi.ForeColor = Color.Red;
}