winforms 如何根据现有文件名创建文件名?

jxct1oxe  于 2022-11-17  发布在  其他
关注(0)|答案(2)|浏览(174)
private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;

            if (DrawingRects.Count > 0)
            {
                // The last drawn shape
                var dr = DrawingRects.Last();
                if (dr.Rect.Width > 0 && dr.Rect.Height > 0)
                {
                    rectImage = cropAtRect((Bitmap)pictureBox2.Image, dr.Rect);
                    if (saveRectangles)
                    {
                        DirectoryInfo dInfo = new DirectoryInfo(@"d:\rectangles");
                        var files = GetFilesByExtensions(dInfo, ".bmp");
                        if (files.Count() > 0)
                        {
                            foreach (var f in files)
                            {

                            }
                        }

                        rectangleName = @"d:\Rectangles\rectangle" + saveRectanglesCounter + ".bmp";
                        FileList.Add($"{dr.Location}, {dr.Size}", rectangleName);
                        string json = JsonConvert.SerializeObject(
    FileList,
    Formatting.Indented // this for pretty print
);
                        using (StreamWriter sw = new StreamWriter(@"d:\rectangles\rectangles.txt", false))
                        {
                            sw.Write(json);
                            sw.Close();
                        }

                        rectImage.Save(rectangleName);
                        saveRectanglesCounter++;
                    }
                    pixelsCounter = rect.Width * rect.Height;
                    pictureBox1.Invalidate();

                    listBox1.DataSource = FileList.ToList();
                    listBox1.SelectedIndex = listBox1.Items.Count - 1;
                }
            }
        }

我使用的是目录信息和GetFilesByExtensions方法

public IEnumerable<FileInfo> GetFilesByExtensions(DirectoryInfo dir, params string[] extensions)
        {
            if (extensions == null)
                throw new ArgumentNullException("extensions");
            IEnumerable<FileInfo> files = dir.EnumerateFiles();
            return files.Where(f => extensions.Contains(f.Extension));
        }

如果存在现有文件,例如rectangle1.bmp rectangle2.bmp....rectangle7.bmp,则在硬盘上创建新的矩形文件时,我希望它是rectangle8.bmp
现在,它试图创建另一个rectangle1.bmp并给予异常,我不想删除现有文件,而是创建新文件。
并使其尽可能通用。但主要目标是根据现有的文件名创建新的文件名并继续计数。

cl25kdpy

cl25kdpy1#

您可以编写一个方法来检查建议的名称是否存在

string GetNextName(string baseName, string extension)
{
    int counter = 1;
    string nextName = baseName + counter + extension;
    while(File.Exists(nextName))
    {
        counter++;
        nextName = baseName + counter + extension;
    }
    return nextName;
}

这样称呼它:

rectangleName = GetNextName(@"d:\Rectangles\rectangle", ".bmp");
pbwdgjma

pbwdgjma2#

您可以使用linq并在一条语句中完成所有操作,如下所示:

DirectoryInfo di = new  DirectoryInfo(@"D:\rectangles");
    var maxIndex = di.GetFiles().Select(fi => fi.Name.Replace("rectangle","").Replace(".bmp", "")).Max(i => i);

相关问题