Jump to content

how to handel this exception

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Amira

Amira

    Newbie

  • Members
  • Pip
  • 2 posts
Hi

in this code


    DirectoryInfo d = new DirectoryInfo(@"path");

            FileInfo[] infos = d.GetFiles("*");

            int i = 0;

            foreach (FileInfo fi in d.GetFiles())

            {

                i = 0;

                i = i + 1;

                System.IO.File.Move(fi.FullName, Path.Combine(fi.Directory.ToString(), Convert.ToString(i)) + fi.Extension);


}
i have this exception(IOException unhandled)

(The process cannot access the file because it is being used by another process)

in this line of code

 System.IO.File.Move(fi.FullName, Path.Combine(fi.Directory.ToString(), Convert.ToString(i)) + fi.Extension);


#2
logicPwn

logicPwn

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
Try this one out

            DirectoryInfo d = new DirectoryInfo(@"path");

            FileInfo[] infos = d.GetFiles("*");

            foreach (FileInfo fi in d.GetFiles())

            {

                try

                {

                    System.IO.File.Move(fi.FullName, Path.Combine(fi.Directory.ToString(), Convert.ToString(i)) + fi.Extension);

                }

                catch (System.IO.IOException ex)

                {

                    MessageBox.Show("This file is in use. Exception message: " + ex.Message);

                }

                i++;

            }

Edit: If you want it to not do anything you can do

catch { }

Edited by logicPwn, 16 February 2012 - 01:33 AM.

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

#3
Amira

Amira

    Newbie

  • Members
  • Pip
  • 2 posts

logicPwn said:

Try this one out

            DirectoryInfo d = new DirectoryInfo(@"path");

            FileInfo[] infos = d.GetFiles("*");

            int i = 0;

            foreach (FileInfo fi in d.GetFiles())

            {

                try

                {

                    System.IO.File.Move(fi.FullName, Path.Combine(fi.Directory.ToString(), Convert.ToString(i)) + fi.Extension);

                }

                catch (System.IO.IOException ex)

                {

                    MessageBox.Show("This file is in use. Exception message: " + ex.Message);

                }

                i++;

            }

Edit: If you want it to not do anything you can do

catch { }

Thanks logicPwn
but the problem don't solve

#4
logicPwn

logicPwn

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
Your path could be incorrect or all the files are in use
"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

#5
Justin Donohoo

Justin Donohoo

    Newbie

  • Members
  • Pip
  • 2 posts

Amira said:

Hi

in this code


    DirectoryInfo d = new DirectoryInfo(@"path");

            FileInfo[] infos = d.GetFiles("*");

            int i = 0;

            foreach (FileInfo fi in d.GetFiles())

            {

                i = 0;

                i = i + 1;

                System.IO.File.Move(fi.FullName, Path.Combine(fi.Directory.ToString(), Convert.ToString(i)) + fi.Extension);


}
i have this exception(IOException unhandled)

(The process cannot access the file because it is being used by another process)

in this line of code

 System.IO.File.Move(fi.FullName, Path.Combine(fi.Directory.ToString(), Convert.ToString(i)) + fi.Extension);

	DirectoryInfo oDir = new DirectoryInfo(@"path");

    FileInfo[] aFiles = oDir.GetFiles("*");

            

            for(int x = 0; x < aFiles.Length; x++)

			{

				try

				{

					System.IO.File.Move(aFiles[x].FullName, Path.Combine(aFiles[x].Directory.ToString(), Convert.ToString(x + 1)) + aFiles[x].Extension);

				}

				catch(Exception ex)

				{

					//Your Logging Function Here

					//Example: 

					Log(ex.Message + "\r\n" + ex.StackTrace);

					continue;

				}

			}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users