Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

C#
File not moving after print because it being used by another process in c#?


my print code run successfully but my file move code its not running its getting error:
C#
it being used by another process


please help me.
how it can be resolve?

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();
                    MessageBox.Show("Printed Successfully" + " " + DateTime.Now.ToString());
                    string fileName = Path.GetFileName(file);
                    System.IO.File.Move(file, @"C:\PrintedDocument\" + fileName);
                    MessageBox.Show("Moved Successfully" + " " + DateTime.Now.ToString());
                    //proc.Kill();
                }
                else
                {
                    MessageBox.Show("Not File found" + " " + DateTime.Now.ToString());
                }
                

            }
        }
Posted
Updated 9-Nov-16 20:14pm
Comments
Patrice T 10-Nov-16 1:37am    
Same problem as your last question
Agarwal1984 10-Nov-16 1:44am    
ya same question,
I used this link:-https://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument(v=vs.110).aspx
but same problem in my code printed successfully run but my file did not move.
Please help me how to dispose print code when print complete.

1 solution

I explained the problem and suggested what you need to do to fix it yesterday: the problem hasn't changed, and neither has the solution.
The process cannot access the file because it is being used by another process.[^]
You are wasting more time trying to get a bodge to work around a bad solution that you could have spent implementing this properly - and you would have learned something at the same time...

If you are not going to listen to what we tell you, is there any point in you asking a question, or in us answering it?
 
Share this answer
 

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