Ankit Agarwal

Ankit Agarwal

  • NA
  • 379
  • 254.2k

The process cannot access the file because it is being used.

Nov 10 2016 12:09 AM
Hello,
 
 
When run move method so its getting error after print command.
The process cannot access the file because it is being used by another process.
I want to build application for printing process and printed file move to another folder.
Please help me.
How it can be resolve?
Thanks in Advance.
Ankit Agarwal
Software Engineer
Timer myTimer=new Timer();
private void Form1_Load(object sender, EventArgs e)
{
myTimer.Tick+=new EventHandler(myTimer_Tick);
myTimer.Interval=60000;
myTimer.Start();
}
private void myTimer_Tick(object sender, EventArgs e)
{
string[] files = Directory.GetFiles(@"C:\PrintingDocument\");
foreach (string file in files)
{
//if (string.IsNullOrEmpty(file))
//{
// //txtFileName.BackColor = Color.Yellow;
// //MessageBox.Show("Please Select file.");
// return;
//}
if (File.Exists(file))
{
Process proc = new Process();
proc.StartInfo.FileName = file;
proc.StartInfo.Verb = "Print";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
int exitcode = proc.ExitCode;
proc.Close();
string fileName = Path.GetFileName(file);
System.IO.File.Move(file, @"C:\PrintedDocument\" + fileName);// Error: The process cannot access the file because it is being used by another process.
MessageBox.Show("Printed Successfully" + " " + DateTime.Now.ToString());
}
else
{
MessageBox.Show("Not File found" + " " + DateTime.Now.ToString());
}
}
}

Answers (1)