Click here to Skip to main content
14,981,762 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.
   
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...
Agarwal1984 9-Nov-16 6:47am
   
I have debugged my code and i have written WaitForExit, WaitForExit was calling after printing command run.
and then i am closing process also.
OriginalGriff 9-Nov-16 6:57am
   
Just because the process you spawned is complete, doesn't mean that it hasn't spawned more processes of it's own to actually do the print operation. And if they are holding the file until the paper comes out of the printer for example, then the file is in use until that time.
You are much better off not relying on external processes, but handling the file print yourself. That way, you know when the file is in use, and exactly when it's finished with...
Agarwal1984 9-Nov-16 7:01am
   
So, what can i do for that, because i will be run application on server end without any UI or Button click event.
It will be run automatically. On client computer request.
OriginalGriff 9-Nov-16 7:33am
   
Why do you assume that if an example shows a click event handler that's the only thing you can use it with?
When you watch a car ad on TV, and it shows people driving around Mumbai as if there was no traffic just for the fun of it, do you assume you can't drive that car in a small town? Or on a motorway? Or the School run?
Agarwal1984 9-Nov-16 7:50am
   
I have tried windows service also for that functionality but Process did not run in windows service.
File was moving through windows service but not printing.
OriginalGriff 9-Nov-16 8:18am
   
So. Don't. Use. A. Process.
Agarwal1984 9-Nov-16 23:24pm
   
so, what can we use in window service?

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