Click here to Skip to main content
15,887,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

When run move method so its getting error after print command.
C#
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

What I have tried:

C#
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());
                }

            }
        }
Posted
Updated 8-Nov-16 23:48pm

1 solution

Your Process will exit when it has started the Print task - that doesn't mean that the print operation is complete, that's down to the actual code that "print" uses to transfer the file to the printer.

Since the Windows print command only transfers text files, why not do the print yourself via a PrintDocument class instance instead, and gain a whole load of control over exactly what and how it is printed?
PrintDocument Class (System.Drawing.Printing)[^] The link includes a basic example.
 
Share this answer
 
Comments
#realJSOP 9-Nov-16 6:00am    
Good answer - there's even sample code.
Agarwal1984 9-Nov-16 6:06am    
I don't want to use any click event i want to use only timer basis.
like, when i tried but its getting error.
OriginalGriff 9-Nov-16 6:12am    
Why would you have to use a "click event" - that's sample code to show the class, not Microsoft telling you exactly how you should write your application...
Agarwal1984 9-Nov-16 6:25am    
My document successfully print but my file was not moving in another folder.
OriginalGriff 9-Nov-16 6:42am    
Yes, I know.
That's because it was in use while it is printing...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900