Click here to Skip to main content
15,886,049 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am Develop one application read csv files dynamically from given directory.after read that file move another folder.i am write code to read csv and move to another folder.csv file read perfect after file does not move to another folder given exception : the process cannot access the file because it is being used by another process. the process cannot access the file because it is being used by another process. . i am attach my code below Please solve this problem.
thank you
regards,
raju

code :
C#
try
           {
               DialogResult result = folderBrowserDialog1.ShowDialog();

               if (result == DialogResult.OK)
               {
                   string abc = folderBrowserDialog1.SelectedPath;
                   DirectoryInfo selectedPath = new DirectoryInfo(abc);
                   textBox1.Text = abc;
                   //string[] fileList = System.IO.Directory.GetFiles(abc, "*.csv");
                   // using (System.IO.StreamReader sr = new System.IO.StreamReader("c:/Raj/abcd.txt"))
                   {
                       for (int i = 0; i != null; i++)
                       {
                           string[] fileList = System.IO.Directory.GetFiles(abc, "*.csv");

                           string abcd = fileList[i];

                           StreamReader sr = new System.IO.StreamReader(abcd);

                           string currentLine;
                           while ((currentLine = sr.ReadLine()) != null)
                           {
                               MessageBox.Show(currentLine);
                               textBox2.Text = currentLine;

                               string destinationPath = @"c:\Raj\log";
                               string fileToMove = abcd;
                               string moveTo = destinationPath;
                               File.Move(fileToMove, moveTo);
                               MessageBox.Show("file moved");

                           }

                       }
                   }
               }
           }
           catch (Exception e)
           {
               MessageBox.Show(e.Message);

           }
Posted

1 solution

the thing is, in the code block
C#
while ((currentLine = sr.ReadLine()) != null)
                            {
                                MessageBox.Show(currentLine);
                                textBox2.Text = currentLine;
 
                                string destinationPath = @"c:\Raj\log";
                                string fileToMove = abcd;
                                string moveTo = destinationPath;
                                File.Move(fileToMove, moveTo);
                                MessageBox.Show("file moved");
 
                            }

you read one line, then even before reading second line, you try to move the file. I advise to relocate the lines
C#
string destinationPath = @"c:\Raj\log";
                                string fileToMove = abcd;
                                string moveTo = destinationPath;
                                File.Move(fileToMove, moveTo);
                                MessageBox.Show("file moved");

out of the while block:
C#
while ((currentLine = sr.ReadLine()) != null)
                            {
                                MessageBox.Show(currentLine);
                                textBox2.Text = currentLine;
 
                            }
sr.Close();
string destinationPath = @"c:\Raj\log";
                                string fileToMove = abcd;
                                string moveTo = destinationPath;
                                File.Move(fileToMove, moveTo);
                                MessageBox.Show("file moved");
 
Share this answer
 
v2

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