如何从FileInfo []获取文件名?

时间:2012-10-31 17:36:25

标签: c#

我有这段代码:

_fi = new DirectoryInfo (subDirectoryName).GetFiles("*.bmp");
label15.Text = _fi.Length.ToString();
for (int i = 0; i < myNumbers.Count; i++)
{
    if (myNumbers[i] >= max_min_threshold)
    {
        FileName = i.ToString("D6") + ".bmp";
        if (File.Exists(subDirectoryName + "\\" + FileName))
           ...
    }
}

现在_fi在每个索引中包含2255个索引,例如在index [0]中有一个文件名,我看到: {} 000001.bmp 在索引[1] {000002.bmp}

然后我运行myNumbers,每个包含2256个索引,每个索引都有一个数字。例如,在索引[0]中,索引[1]中有225000,其中有223000,依此类推。

我想要做的是使用这个循环或其他方式,每次迭代都会将_fi变量中的文件名转换为FileName变量。

因此在ifrst变量中,FileName将在第二次迭代中为000001.bmp,FileName将为000002.bmp,依此类推。

问题是FileName现在在第一次迭代中将是000000.bmp在硬盘中不存在。我在_fi中看到它的第一个文件是000001.bmp

那么我怎样才能让它成功呢?

感谢。

编辑:

试图这样做:

if (i == _fi.Length)
                            {
                                break;
                            }
                            FileName = (i + 1).ToString("D6") + ".bmp";

问题是在休息之后它会进入这个部分:

button5.Enabled = true;
                        myTrackPanelss1.trackBar1.Maximum = counter;
                        myTrackPanelss1.trackBar1.Value = 0;
                        setpicture(0);

现在setpicture(0);将在pictureBox1中显示帧/图像编号1,它是fike:000001.bmp 这是setpicture函数:

private void setpicture(int indx)
        {
            if (_fi == null)
            {
                pictureBox1.Image = Lightnings_Extractor.Properties.Resources.Weather_Michmoret;
                button5.Enabled = false;
                label5.Visible = true;
            }
            else
            {
                if (indx >= 0 && indx <= myTrackPanelss1.trackBar1.Maximum && _fi.Length > indx)
                {
                    try
                    {
                        label19.ForeColor = Color.Red;
                        fileToolStripMenuItem.Enabled = true;
                        label19.Visible = false;
                        label20.Visible = false;
                        label14.Visible = true;
                        label15.Visible = true;
                        label8.Visible = true;
                        label9.Visible = true;
                        myTrackPanelss1.trackBar1.Enabled = true;
                        using (FileStream fs = new FileStream(_fi[indx].FullName, FileMode.Open))
                        {
                            this.label8.Visible = true;
                            this.label9.Visible = true;
                            this.label9.Text = _fi[indx].Name;
                            Image img = null;
                            Bitmap bmp = null;
                            Image imgOLd = null;

                            try
                            {

                                img = Image.FromStream(fs);
                                bmp = new Bitmap(img);

                                imgOLd = this.pictureBox1.Image;
                                this.pictureBox1.Image = bmp;
                                if (imgOLd != null)
                                    imgOLd.Dispose();

                                img.Dispose();
                                img = null;
                            }
                            catch
                            {
                                if (img != null)
                                    img.Dispose();
                                if (bmp != null)
                                    bmp.Dispose();
                                if (imgOLd != null)
                                    imgOLd.Dispose();
                            }
                        }
                    }
                    catch
                    {
                        button1.Enabled = false;
                        label1.Visible = false;
                        label2.Visible = false;
                        label3.Visible = false;
                        label4.Visible = false;
                        label11.Visible = false;
                        label12.Visible = false;
                        checkBox2.Enabled = false;
                        label19.Visible = true;
                        label19.ForeColor = Color.Green;
                        label19.Text = "The Selected Directory Is Access Denied";
                        label20.Visible = true;
                        label20.ForeColor = Color.Green;
                        label20.Text = "Please Set A Different Directory";
                        fileToolStripMenuItem.Enabled = false;
                        label14.Visible = false;
                        label15.Visible = false;
                        label8.Visible = false;
                        label9.Visible = false;
                        myTrackPanelss1.trackBar1.Enabled = false;
                        timer2.Stop();
                        return;
                    }
                }
                else
                {
                    Image imgOLd = this.pictureBox1.Image;
                    //this.pictureBox1.Image = null;

                    if (imgOLd != null)
                    {
                        imgOLd.Dispose();
                        imgOLd = null;
                    }

                    Application.DoEvents();
                }
            }
        }

在线:

using (FileStream fs = new FileStream(_fi[indx].FullName, FileMode.Open))

我现在变得异常了,因为我没有做过brek;如果我== _fi.Length但是在我做了它之后我会在这条线上获得异常,它会移动到捕获区并抛弃我:

该进程无法访问文件'D:\ New folder(7)\ MVI_3041.MOV_Automatic \ 000001.bmp',因为它正由另一个进程使用。

System.IO.IOException was caught
  Message=The process cannot access the file 'D:\New folder (7)\MVI_3041.MOV_Automatic\000001.bmp' because it is being used by another process.
  Source=mscorlib
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode)
       at Extracting_Frames.Form1.setpicture(Int32 indx) in D:\C-Sharp\Extracting_Frames\Extracting_Frames\Extracting_Frames\Form1.cs:line 508
  InnerException: 

我不确定为什么我现在才得到这个例外。

6 个答案:

答案 0 :(得分:1)

如果你真的想从FileInfo []获取文件名:

string[] names = new DirectoryInfo(subDirectoryName)
        .GetFiles("*.bmp")
        .Select(fi => fi.Name)
        .ToArray();

答案 1 :(得分:1)

像这样创建文件名

FileName = (i + 1).ToString("D6") + ".bmp";

更新

为了访问FileInfo中的_fi,我会将它们添加到词典中以便快速查找。

Dictionary<string, FileInfo> fileDict = _fi.ToDictionary(f => f.Name);

(一定要using System.Linq;。)

然后您可以使用

查找文件
FileInfo fi;
if (fileDict.TryGetValue(FileName, out fi)) {
    DoSomethingWithTheFile(fi);
}

这比调用if (File.Exists(...))

快得多

答案 2 :(得分:0)

启动for循环为1而不是0.这就是为什么它首先要查找000000.bmp。

编辑:要执行此操作,您需要更改数组引用。

答案 3 :(得分:0)

看起来你只需要更新打印,这样我就可以从1而不是0开始,如果你知道你的第一个文件名是.000001.bmp。 (我在这里假设你的“D6”覆盖ToString只是将i的值左移6位。

所以代替i.toString(“D6”)尝试(i + 1).toString(“D6”)。

这是快而又脏的。更“干净”的方式是在上面的循环中以1开始,但是你必须改变数组引用以不错过0索引

答案 4 :(得分:0)

也许我不明白你的问题,但如果你只想迭代文件列表,你可以这样做......

_fi = new DirectoryInfo (subDirectoryName).GetFiles("*.bmp");

foreach (var file in _fi)
{
    FileName = file.Name;

    // The rest of your code...
}

答案 5 :(得分:0)

找到解决方案:

using (FileStream fs = new FileStream(_fi[indx].FullName,FileMode.Open, FileAccess.Read, FileShare.ReadWrite))

现在正在运作。

谢谢大家。

相关问题