c# 当我将文件复制到文件夹时,图像文件被复制
c# the image file is duplicated when I copy file to the folder
我正在尝试浏览多个图像文件并将它们复制到另一个文件夹。所以,我所做的是,当我浏览我存储在 flowlayoutpanel 中的图像时,我创建了一个按钮,将所有图像从 flowlayutpanel 复制到目标文件夹。现在,当我复制到另一个文件夹时出现问题,即使它具有不同的文件名。解决这个问题的任何建议。
*这是将文件复制到文件夹的代码。
private void button2_Click(object sender, EventArgs e)
{
foreach (TextBox tb1 in TextBoxes1)
{
MessageBox.Show(tb1.Text);
String[] files = openFileDialog1.FileNames;
String newDir = @"C:\Users\Public\Pictures\Sample Pictures\Modified";
System.IO.Directory.CreateDirectory(newDir);
Parallel.ForEach(files, (textboxes) =>
{
foreach (TextBox tb in TextBoxes)
{
String filename = System.IO.Path.GetFileName(tb.Text);
var bitmap = new Bitmap(textboxes);
String text = Path.Combine(newDir, filename);
string toDisplay = string.Join(Environment.NewLine, files);
MessageBox.Show(toDisplay);
bitmap.Save(text);
}
});
}
}
* 这是我如何浏览图像文件并在 flowlayout 中显示的代码
private void button1_Click_2(object sender, EventArgs e)
{
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
// Read the files
foreach (String file in openFileDialog1.FileNames)
{
// Create a PictureBox.
try
{
Panel panel = new Panel();
PictureBox pb = new PictureBox();
TextBox tb = new TextBox();
TextBox tb1 = new TextBox();
Image loadedImage = Image.FromFile(file);
pb.Height = 200;
pb.Width = 200;
pb.Image = loadedImage;
pb.SizeMode = PictureBoxSizeMode.StretchImage;
tb.Text = Path.GetFileName(openFileDialog1.FileName);
tb1.Text = openFileDialog1.FileName;
//panel.Controls.Add(pb);
panel.Controls.Add(tb);
panel.Controls.Add(tb1);
panel.Height = 200;
panel.Width = 200;
flowLayoutPanel1.Controls.Add(panel);
TextBoxes.Add(tb);
TextBoxes1.Add(tb1);
}
catch (SecurityException ex)
{
// The user lacks appropriate permissions to read files, discover paths, etc.
MessageBox.Show("Security error. Please contact your administrator for details.\n\n" +
"Error message: " + ex.Message + "\n\n" +
"Details (send to Support):\n\n" + ex.StackTrace
);
}
catch (Exception ex)
{
// Could not load the image - probably related to Windows file system permissions.
MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\'))
+ ". You may not have permission to read the file, or " +
"it may be corrupt.\n\nReported error: " + ex.Message);
}
}
}
我猜 parallel.foreach 是罪魁祸首。使用 Parallel.foreach 时您正在使用多个线程
换句话说,你有两个线程在做同样的事情。
我建议您尝试使用简单的 foreach 循环。
我正在尝试浏览多个图像文件并将它们复制到另一个文件夹。所以,我所做的是,当我浏览我存储在 flowlayoutpanel 中的图像时,我创建了一个按钮,将所有图像从 flowlayutpanel 复制到目标文件夹。现在,当我复制到另一个文件夹时出现问题,即使它具有不同的文件名。解决这个问题的任何建议。
*这是将文件复制到文件夹的代码。
private void button2_Click(object sender, EventArgs e)
{
foreach (TextBox tb1 in TextBoxes1)
{
MessageBox.Show(tb1.Text);
String[] files = openFileDialog1.FileNames;
String newDir = @"C:\Users\Public\Pictures\Sample Pictures\Modified";
System.IO.Directory.CreateDirectory(newDir);
Parallel.ForEach(files, (textboxes) =>
{
foreach (TextBox tb in TextBoxes)
{
String filename = System.IO.Path.GetFileName(tb.Text);
var bitmap = new Bitmap(textboxes);
String text = Path.Combine(newDir, filename);
string toDisplay = string.Join(Environment.NewLine, files);
MessageBox.Show(toDisplay);
bitmap.Save(text);
}
});
}
}
* 这是我如何浏览图像文件并在 flowlayout 中显示的代码
private void button1_Click_2(object sender, EventArgs e)
{
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
// Read the files
foreach (String file in openFileDialog1.FileNames)
{
// Create a PictureBox.
try
{
Panel panel = new Panel();
PictureBox pb = new PictureBox();
TextBox tb = new TextBox();
TextBox tb1 = new TextBox();
Image loadedImage = Image.FromFile(file);
pb.Height = 200;
pb.Width = 200;
pb.Image = loadedImage;
pb.SizeMode = PictureBoxSizeMode.StretchImage;
tb.Text = Path.GetFileName(openFileDialog1.FileName);
tb1.Text = openFileDialog1.FileName;
//panel.Controls.Add(pb);
panel.Controls.Add(tb);
panel.Controls.Add(tb1);
panel.Height = 200;
panel.Width = 200;
flowLayoutPanel1.Controls.Add(panel);
TextBoxes.Add(tb);
TextBoxes1.Add(tb1);
}
catch (SecurityException ex)
{
// The user lacks appropriate permissions to read files, discover paths, etc.
MessageBox.Show("Security error. Please contact your administrator for details.\n\n" +
"Error message: " + ex.Message + "\n\n" +
"Details (send to Support):\n\n" + ex.StackTrace
);
}
catch (Exception ex)
{
// Could not load the image - probably related to Windows file system permissions.
MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\'))
+ ". You may not have permission to read the file, or " +
"it may be corrupt.\n\nReported error: " + ex.Message);
}
}
}
我猜 parallel.foreach 是罪魁祸首。使用 Parallel.foreach 时您正在使用多个线程 换句话说,你有两个线程在做同样的事情。
我建议您尝试使用简单的 foreach 循环。