C# 如何修复进程无法访问该文件,因为该文件正被其他进程使用
我写了一个小代码在不同的屏幕上制作幻灯片程序。一切都很好,删除和复制部分也很好,除非我在第一张图片显示后立即执行。如果我以后再这样做,调试器就会介入C# 如何修复进程无法访问该文件,因为该文件正被其他进程使用,c#,winforms,timer,picturebox,C#,Winforms,Timer,Picturebox,我写了一个小代码在不同的屏幕上制作幻灯片程序。一切都很好,删除和复制部分也很好,除非我在第一张图片显示后立即执行。如果我以后再这样做,调试器就会介入 private void timer1_Tick(object sender, EventArgs e) { string[] images = Directory.GetFiles(@"D:\reflexscherm\root\Logo-teams2", "*.*"); counter++; var maxcount =
private void timer1_Tick(object sender, EventArgs e)
{
string[] images = Directory.GetFiles(@"D:\reflexscherm\root\Logo-teams2", "*.*");
counter++;
var maxcount = images.Count();
textBox1.Text = maxcount.ToString();
if (counter > maxcount - 1)
{
counter = 0;
maxcount = images.Count();
}
//pb1.Image.Dispose();
pb1.Image = Image.FromFile(images[counter]);
//Image oldImage = pb1.Image;
//pb1.Image.Dispose();
//oldImage.Dispose();
//pb1.Image = Image.FromFile(images[counter]);
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
Image oldImage = pb1.Image;
pb1.Image = Image.FromFile(@"D:\reflexscherm\root\sponsor1\x. Groot-logo-REFLEX.jpg");
pb1.Image.Dispose();
oldImage.Dispose();
string[] files = System.IO.Directory.GetFiles(sourcepath);
string[] delfiles = Directory.GetFiles(targetpath);
this.Hide();
foreach (string d in delfiles)
{
Image oldI = pb1.Image;
pb1.Image = Image.FromFile(@"D:\reflexscherm\root\sponsor1\x. Groot-logo-REFLEX.jpg");
//pb1.Image.Dispose();
oldI.Dispose();
File.Delete(d);
}
foreach (string s in files)
{
string fname = s.Substring(sourcepath.Length + 1);
File.Copy(Path.Combine(sourcepath, fname), Path.Combine(targetpath, fname), true);
this.Show();
timer1.Start();
}
我正在寻找一些帮助来调整代码,因此当我更改sourcefolder中的文件时,程序会将文件从sourcefolder复制到targetfolder。我知道如何使用filewatcher。我正在使用一个按钮测试代码。我以前也遇到过同样的问题,我做了以下操作:
this.photo.Dispose();
this.photo.Refresh();
this.photo.Image.Dispose();
this.photo.Image = null;
this.photo.ImageLocation = null;
这是值得的。我以前也遇到过同样的问题,我做了以下几件事:
this.photo.Dispose();
this.photo.Refresh();
this.photo.Image.Dispose();
this.photo.Image = null;
this.photo.ImageLocation = null;
这是值得的。如果不想锁定文件,则应使用只读文件访问:
using( FileStream stream = new FileStream( path, FileMode.Open, FileAccess.Read ) )
{
image = Image.FromStream( stream );
}
希望有帮助…如果不想锁定文件,则应使用只读文件访问:
using( FileStream stream = new FileStream( path, FileMode.Open, FileAccess.Read ) )
{
image = Image.FromStream( stream );
}
希望它能帮助…方法1:保留副本并分配控件的属性 当位图对象在多个位置进行处理时,请使用此方法,因此我们需要将其保留以供进一步细化/分配,并在处理后保存到光盘 立即分配、存储和处置源位图,删除图像文件:
Bitmap bitmap = null;
//---------------------------------------------
string imagePath = @"[Path of the Image]";
bitmap?.Dispose();
pictureBox1.Image?.Dispose();
using (Bitmap tempImage = new Bitmap(imagePath, true))
{
bitmap = new Bitmap(tempImage);
pictureBox1.Image = bitmap;
}
File.Delete(imagePath);
方法2:分配位图并立即处理它
当需要将位图指定给控件,然后移动/删除图像文件时,可以使用此方法。图像会立即处理,因此只能通过控件的属性才能使用:如果我们要求将其取回,有时我们得到的并不完全是我们给予的
string imagePath = @"[Path of the Image]";
using (Image image = Image.FromFile(imagePath, true))
{
pictureBox1.Image?.Dispose();
pictureBox1.Image = new Bitmap(image);
}
File.Delete(imagePath);
请注意,分配给控件的旧图像(如果有)在分配新图像之前被处理
还要注意,我总是指定保留内部ICM信息(如果有),指定true
作为新位图(imagePath,true)
和Image.FromFile(imagePath,true)
的第二个参数如果我们不这样做,有些图像就不会像原来那样了 方法1:保留副本并分配控件的属性 当位图对象在多个位置进行处理时,请使用此方法,因此我们需要将其保留以供进一步细化/分配,并在处理后保存到光盘 立即分配、存储和处置源位图,删除图像文件:
Bitmap bitmap = null;
//---------------------------------------------
string imagePath = @"[Path of the Image]";
bitmap?.Dispose();
pictureBox1.Image?.Dispose();
using (Bitmap tempImage = new Bitmap(imagePath, true))
{
bitmap = new Bitmap(tempImage);
pictureBox1.Image = bitmap;
}
File.Delete(imagePath);
方法2:分配位图并立即处理它
当需要将位图指定给控件,然后移动/删除图像文件时,可以使用此方法。图像会立即处理,因此只能通过控件的属性才能使用:如果我们要求将其取回,有时我们得到的并不完全是我们给予的
string imagePath = @"[Path of the Image]";
using (Image image = Image.FromFile(imagePath, true))
{
pictureBox1.Image?.Dispose();
pictureBox1.Image = new Bitmap(image);
}
File.Delete(imagePath);
请注意,分配给控件的旧图像(如果有)在分配新图像之前被处理
还要注意,我总是指定保留内部ICM信息(如果有),指定true
作为新位图(imagePath,true)
和Image.FromFile(imagePath,true)
的第二个参数如果我们不这样做,有些图像就不会像原来那样了 谢谢你的回答,但它对我不起作用:(你是按同样的顺序做的吗?谢谢你的回答,但它对我不起作用。)(你是按同样的顺序做的吗?