IOException and Number formatting
-
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 itprivate 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 andextension
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 thestring.Forma
t 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.
-
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 itprivate 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 andextension
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 thestring.Forma
t 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.
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.
-
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 itprivate 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 andextension
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 thestring.Forma
t 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.
-
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 itprivate 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 andextension
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 thestring.Forma
t 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.
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.
-
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 itprivate 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 andextension
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 thestring.Forma
t 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.
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.”
-
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.”
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.
-
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.
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.”
-
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);
-
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.”
-
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 itprivate 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 andextension
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 thestring.Forma
t 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.
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.