I have two simple Console APPs. Program1 calls Program2 to do simple moving of 500 files to another folder.
Program1.exe:
static void Main(string[] args)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
proc.EnableRaisingEvents = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = @"D:\Program2.exe";
proc.Start();
proc.WaitForExit();
proc.Close();
}
Program2.exe:
static void Main(string[] args)
{
string srcDic= @"D:\docs";
var DesDic= @"D:\docs2";
var pdf_files = Directory.GetFiles(srcDic, "*.pdf", SearchOption.TopDirectoryOnly);
foreach (var pdf_file in pdf_files)
{
var fileName = new DirectoryInfo(pdf_file).Name;
File.Move(pdf_file, DesDic + "\\" + fileName);
Console.WriteLine("Moving " + pdf_file);
}
Console.WriteLine("----- All files moved!");
}
Issue: When I run Program1.exe to run Program2, most of the time Program2 will be frozen and doesn't move all the files. I have to close it manually.
Note:
- When I run Program2.exe without Program1, it works great and moves all the files.
- I don't have Anti-Virus, and I even turned off the Windows Defender Firewall
- This code is part of a big software, and I don't want to write the move code directly in Program1