简体   繁体   English

如何检查文件是否已经存在?

[英]How can I check if a file already exists?

public void SaveFormPicutreBoxToBitMapIncludingDrawings()
{
    using (Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height))
    {
        pictureBox1.DrawToBitmap(b, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
        string fn = @"d:\PictureBoxToBitmap\" + PbToBitmap.ToString("D6") + ".bmp";
        if (File.Exists(fn))
        {
        }
        else
        {
            b.Save(@"d:\PictureBoxToBitmap\" + PbToBitmap.ToString("D6") + ".bmp"); // to fix/repair give gdi error exception.
            PbToBitmap++;
        }
    } 
}

If I move the trackBar to the right it will save the first file 000000.bmp then raise it by one next time it will save file 000001.bmp如果我将 trackBar 向右移动,它将保存第一个文件 000000.bmp,然后将其提高一个,下次它将保存文件 000001.bmp

But if I move back to the left once now the variable fn is 000002.bmp which doesn't exist and it will save the previous image which is really 000001.bmp.但是,如果我现在向左移动一次,变量 fn 是 000002.bmp,它不存在,它将保存之前的图像,实际上是 000001.bmp。

What it suppose to do when I move to the left back it should be 000001.bmp see that it exist and do nothing.当我向左移动时它应该做什么应该是 000001.bmp 看到它存在并且什么也不做。

If will not make this checking it will just keep saving files all the time if i move the trackBar to the right or to the left so after few times i will have more then 90 files which are almost all the same.如果不进行此检查,如果我将 trackBar 向右或向左移动,它将一直保存文件,所以几次后我将拥有超过 90 个几乎完全相同的文件。

How can I solve it?我该如何解决?

The variable PbtoBitmap is type of int in the top of Form1 I just started it with 0. PbToBitmap = 0;变量 PbtoBitmap 是 Form1 顶部的 int 类型,我刚从 0 开始。 PbToBitmap = 0;

The trackBar I'm talking about is trackBar1 where in the scroll event I'm calling this SaveFormPicutreBoxToBitMapIncludingDrawings function.我所说的 trackBar 是 trackBar1,在滚动事件中我调用了这个 SaveFormPicutreBoxToBitMapIncludingDrawings 函数。

This is the trackBar1 scroll event:这是 trackBar1 滚动事件:

private void trackBar1_Scroll(object sender, EventArgs e)
{
    currentFrameIndex = trackBar1.Value;
    textBox1.Text = "Frame Number : " + trackBar1.Value;
    wireObject1.woc.Set(wireObjectAnimation1.GetFrame(currentFrameIndex)); 

    trackBar1.Minimum = 0;
    trackBar1.Maximum = fi.Length - 1;
    setpicture(trackBar1.Value);
    pictureBox1.Refresh();

    button1.Enabled = false;
    button2.Enabled = false;
    button3.Enabled = false;
    button4.Enabled = false;
    button8.Enabled = false;
    SaveFormPicutreBoxToBitMapIncludingDrawings();
    return;
}

It is not clear what you are trying to accomplish, but you are generating file names from a counter PbToBitmap .目前尚不清楚您要完成什么,但您正在从计数器PbToBitmap生成文件名。 This counter only increases and never decreases so of course it will "just keep saving files... ".这个计数器只会增加而不会减少,所以它当然会“继续保存文件……”。

If you want the counter to match the frame you're on, get rid of PbToBitmap and inside trackBar1_Scroll call:如果您希望计数器与您所在的帧相匹配,请删除PbToBitmap并在trackBar1_Scroll调用内部:

string dir = @"d:\PictureBoxToBitmap";
SaveFormPictureBoxToBitMapIncludingDrawings(dir, currentFrameIndex);

Change your SaveFormPictureBoxToBitMapIncludingDrawings to:将您的SaveFormPictureBoxToBitMapIncludingDrawings更改为:

public void SaveFormPictureBoxToBitMapIncludingDrawings(string dir, int frameIndex)
{
    string fn = Path.Combine(dir, frameIndex.ToString("D6") + ".bmp");
    if (!File.Exists(fn))
    {
        using (Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height))
        {
            pictureBox1.DrawToBitmap(b, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
            b.Save(fn);
        }
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM