Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. IOException and Number formatting

IOException and Number formatting

Scheduled Pinned Locked Moved C#
helpdatabasealgorithms
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Megidolaon
    wrote on last edited by
    #1

    I'm writing a small program to help me sort my thousands of images. But whenever I want to rename a file, I get the following error: The process cannot access the file because it is being used by another process. I use the following code for it

    private void B_Rename_Click(object sender, EventArgs e)
    {
    L_Status.Text = "";
    File.Move(Path.GetFullPath(images[current]), Path.GetFullPath(TB_New_Name.Text));
    L_Status.Text = "image '" + images[current] + "." + extension +
    "'\r\nhas been renamed to\r\n'" +
    TB_New_Name.Text + "." + extension + "'";
    images[current] = TB_New_Name.Text;
    }

    images is the string list holding the file names, current is the current index and extension is the file extension. My other problem is the number formatting. The images should be named with numbers, but since windows sorting is strictly text based, 2 would come after 10. So, I tried the string.Format method to name the images 001, 002, etc., but it doesn't quite work.

    public F_Main()
    {
    InitializeComponent();
    files = Directory.GetFiles(dir);
    this.Text = string.Format("{0:00}", 1);
    this.Text += " - ";
    this.Text = string.Format("{0:00}", 12);
    this.Text += " - ";
    this.Text += string.Format("{0:00}", 120);
    }

    I thought this code would display "001 - 012 - 120" in the form title, but instead it onlydisplays "12 - 120" Thanks.

    L L M H M 5 Replies Last reply
    0
    • M Megidolaon

      I'm writing a small program to help me sort my thousands of images. But whenever I want to rename a file, I get the following error: The process cannot access the file because it is being used by another process. I use the following code for it

      private void B_Rename_Click(object sender, EventArgs e)
      {
      L_Status.Text = "";
      File.Move(Path.GetFullPath(images[current]), Path.GetFullPath(TB_New_Name.Text));
      L_Status.Text = "image '" + images[current] + "." + extension +
      "'\r\nhas been renamed to\r\n'" +
      TB_New_Name.Text + "." + extension + "'";
      images[current] = TB_New_Name.Text;
      }

      images is the string list holding the file names, current is the current index and extension is the file extension. My other problem is the number formatting. The images should be named with numbers, but since windows sorting is strictly text based, 2 would come after 10. So, I tried the string.Format method to name the images 001, 002, etc., but it doesn't quite work.

      public F_Main()
      {
      InitializeComponent();
      files = Directory.GetFiles(dir);
      this.Text = string.Format("{0:00}", 1);
      this.Text += " - ";
      this.Text = string.Format("{0:00}", 12);
      this.Text += " - ";
      this.Text += string.Format("{0:00}", 120);
      }

      I thought this code would display "001 - 012 - 120" in the form title, but instead it onlydisplays "12 - 120" Thanks.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, 1. there are a couple of ways to keep a file "in use by another process", and you should, for once, not take "another process" literally. Here are some: - another process has opened the very same file - for text files that other process could be an automatic indexer - for several extensions that other process could be an antivirus checking a newly created file - MOST LIKELY IN YOUR CASE: an image loaded with Image.FromFile() remains locked as long as the image lives. 2. numeric format "D3" would always use at least 3 characters, filling with leading zeroes. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


      1 Reply Last reply
      0
      • M Megidolaon

        I'm writing a small program to help me sort my thousands of images. But whenever I want to rename a file, I get the following error: The process cannot access the file because it is being used by another process. I use the following code for it

        private void B_Rename_Click(object sender, EventArgs e)
        {
        L_Status.Text = "";
        File.Move(Path.GetFullPath(images[current]), Path.GetFullPath(TB_New_Name.Text));
        L_Status.Text = "image '" + images[current] + "." + extension +
        "'\r\nhas been renamed to\r\n'" +
        TB_New_Name.Text + "." + extension + "'";
        images[current] = TB_New_Name.Text;
        }

        images is the string list holding the file names, current is the current index and extension is the file extension. My other problem is the number formatting. The images should be named with numbers, but since windows sorting is strictly text based, 2 would come after 10. So, I tried the string.Format method to name the images 001, 002, etc., but it doesn't quite work.

        public F_Main()
        {
        InitializeComponent();
        files = Directory.GetFiles(dir);
        this.Text = string.Format("{0:00}", 1);
        this.Text += " - ";
        this.Text = string.Format("{0:00}", 12);
        this.Text += " - ";
        this.Text += string.Format("{0:00}", 120);
        }

        I thought this code would display "001 - 012 - 120" in the form title, but instead it onlydisplays "12 - 120" Thanks.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Check if the "image" file you want to move is already opened from somewhere else. The second problem is : See this line : this.Text = string.Format("{0:00}", 12); it has to be this.Text += string.Format("{0:00}", 12);

        M 1 Reply Last reply
        0
        • M Megidolaon

          I'm writing a small program to help me sort my thousands of images. But whenever I want to rename a file, I get the following error: The process cannot access the file because it is being used by another process. I use the following code for it

          private void B_Rename_Click(object sender, EventArgs e)
          {
          L_Status.Text = "";
          File.Move(Path.GetFullPath(images[current]), Path.GetFullPath(TB_New_Name.Text));
          L_Status.Text = "image '" + images[current] + "." + extension +
          "'\r\nhas been renamed to\r\n'" +
          TB_New_Name.Text + "." + extension + "'";
          images[current] = TB_New_Name.Text;
          }

          images is the string list holding the file names, current is the current index and extension is the file extension. My other problem is the number formatting. The images should be named with numbers, but since windows sorting is strictly text based, 2 would come after 10. So, I tried the string.Format method to name the images 001, 002, etc., but it doesn't quite work.

          public F_Main()
          {
          InitializeComponent();
          files = Directory.GetFiles(dir);
          this.Text = string.Format("{0:00}", 1);
          this.Text += " - ";
          this.Text = string.Format("{0:00}", 12);
          this.Text += " - ";
          this.Text += string.Format("{0:00}", 120);
          }

          I thought this code would display "001 - 012 - 120" in the form title, but instead it onlydisplays "12 - 120" Thanks.

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #4

          The error usually occurs when you have a reference to the image still active. How are you loading the 'images' list can you show the code for that please. with regards to the fomatting then try using the int.ToString() method...

          int i = 12;
          string s = i.ToString("000");//s should now = "012"

          Life goes very fast. Tomorrow, today is already yesterday.

          1 Reply Last reply
          0
          • M Megidolaon

            I'm writing a small program to help me sort my thousands of images. But whenever I want to rename a file, I get the following error: The process cannot access the file because it is being used by another process. I use the following code for it

            private void B_Rename_Click(object sender, EventArgs e)
            {
            L_Status.Text = "";
            File.Move(Path.GetFullPath(images[current]), Path.GetFullPath(TB_New_Name.Text));
            L_Status.Text = "image '" + images[current] + "." + extension +
            "'\r\nhas been renamed to\r\n'" +
            TB_New_Name.Text + "." + extension + "'";
            images[current] = TB_New_Name.Text;
            }

            images is the string list holding the file names, current is the current index and extension is the file extension. My other problem is the number formatting. The images should be named with numbers, but since windows sorting is strictly text based, 2 would come after 10. So, I tried the string.Format method to name the images 001, 002, etc., but it doesn't quite work.

            public F_Main()
            {
            InitializeComponent();
            files = Directory.GetFiles(dir);
            this.Text = string.Format("{0:00}", 1);
            this.Text += " - ";
            this.Text = string.Format("{0:00}", 12);
            this.Text += " - ";
            this.Text += string.Format("{0:00}", 120);
            }

            I thought this code would display "001 - 012 - 120" in the form title, but instead it onlydisplays "12 - 120" Thanks.

            H Offline
            H Offline
            Henry Minute
            wrote on last edited by
            #5

            In addition to Luc's points, for 2.

            public F\_Main()
            {
                InitializeComponent();
                files = Directory.GetFiles(dir);
                this.Text = string.Format("{0:D3} - {1:D3} - {2:D3}", 1, 12, 120);
            }
            

            seems neater and easier to read to me.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            M 1 Reply Last reply
            0
            • H Henry Minute

              In addition to Luc's points, for 2.

              public F\_Main()
              {
                  InitializeComponent();
                  files = Directory.GetFiles(dir);
                  this.Text = string.Format("{0:D3} - {1:D3} - {2:D3}", 1, 12, 120);
              }
              

              seems neater and easier to read to me.

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              M Offline
              M Offline
              musefan
              wrote on last edited by
              #6

              Henry Minute wrote:

              this.Text = string.Format("{0:D3} - {1:D3} - {2:D3}", 1, 12, 120);

              Problem with that is it will only work for a fixed number of files, which anything above 1 seems like too much work for me :) ...Thou given the OP's example it would produce what he was showing. My assumption is that each file with be appended with a number that increments with each file

              Life goes very fast. Tomorrow, today is already yesterday.

              H 1 Reply Last reply
              0
              • M musefan

                Henry Minute wrote:

                this.Text = string.Format("{0:D3} - {1:D3} - {2:D3}", 1, 12, 120);

                Problem with that is it will only work for a fixed number of files, which anything above 1 seems like too much work for me :) ...Thou given the OP's example it would produce what he was showing. My assumption is that each file with be appended with a number that increments with each file

                Life goes very fast. Tomorrow, today is already yesterday.

                H Offline
                H Offline
                Henry Minute
                wrote on last edited by
                #7

                musefan wrote:

                it will only work for a fixed number of files,

                Whilst that is true, I was simply implementing the users own logic, in what I thought was a better way. You are absolutely correct if there were likely to be an unknown number of files.

                Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                M 1 Reply Last reply
                0
                • L Lost User

                  Check if the "image" file you want to move is already opened from somewhere else. The second problem is : See this line : this.Text = string.Format("{0:00}", 12); it has to be this.Text += string.Format("{0:00}", 12);

                  M Offline
                  M Offline
                  musefan
                  wrote on last edited by
                  #8

                  I think your missing the point of the question. I believe the OP was trying to demonstrate that the number formatting did not include the desired prefix of zeros.

                  Life goes very fast. Tomorrow, today is already yesterday.

                  1 Reply Last reply
                  0
                  • H Henry Minute

                    musefan wrote:

                    it will only work for a fixed number of files,

                    Whilst that is true, I was simply implementing the users own logic, in what I thought was a better way. You are absolutely correct if there were likely to be an unknown number of files.

                    Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                    M Offline
                    M Offline
                    musefan
                    wrote on last edited by
                    #9

                    that's why I edited my post as I can see you are just providing a solution based on the OP's example ;)

                    Life goes very fast. Tomorrow, today is already yesterday.

                    1 Reply Last reply
                    0
                    • M Megidolaon

                      I'm writing a small program to help me sort my thousands of images. But whenever I want to rename a file, I get the following error: The process cannot access the file because it is being used by another process. I use the following code for it

                      private void B_Rename_Click(object sender, EventArgs e)
                      {
                      L_Status.Text = "";
                      File.Move(Path.GetFullPath(images[current]), Path.GetFullPath(TB_New_Name.Text));
                      L_Status.Text = "image '" + images[current] + "." + extension +
                      "'\r\nhas been renamed to\r\n'" +
                      TB_New_Name.Text + "." + extension + "'";
                      images[current] = TB_New_Name.Text;
                      }

                      images is the string list holding the file names, current is the current index and extension is the file extension. My other problem is the number formatting. The images should be named with numbers, but since windows sorting is strictly text based, 2 would come after 10. So, I tried the string.Format method to name the images 001, 002, etc., but it doesn't quite work.

                      public F_Main()
                      {
                      InitializeComponent();
                      files = Directory.GetFiles(dir);
                      this.Text = string.Format("{0:00}", 1);
                      this.Text += " - ";
                      this.Text = string.Format("{0:00}", 12);
                      this.Text += " - ";
                      this.Text += string.Format("{0:00}", 120);
                      }

                      I thought this code would display "001 - 012 - 120" in the form title, but instead it onlydisplays "12 - 120" Thanks.

                      M Offline
                      M Offline
                      Megidolaon
                      wrote on last edited by
                      #10

                      Thanks. For the formatting "D3" works the way I want. Though, the main problem is still the renaming. I changed the code for displaying the picture to:

                      Bitmap picture = new Bitmap(800, 600);
                      picture = new Bitmap(images[0]);
                      PB_Image.Image = new Bitmap(picture);
                      picture.Dispose();
                      TB_Old_Name.Text = Path.GetFileNameWithoutExtension(images[0]);

                      And this is the code I use for renaming:

                      L_Status.Text = "";
                      File.Move(Path.GetFullPath(images[current]), Path.GetFullPath(TB_New_Name.Text));
                      L_Status.Text = "image '" + images[current] + "." + extension + "'\r\nhas been renamed to\r\n'"
                      +TB_New_Name.Text + "." + extension + "'";
                      images[current] = TB_New_Name.Text;

                      But now the program simply deletes the image. I tested it and the image that should have been renamed simply disappeared.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups