Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create files names according to existing files names?

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;
                }
            }
        }

I’m using DirectoryInfo and the method 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));
        }

if there are existing files for example rectangle1.bmp rectangle2.bmp…..rectangle7.bmp
then when creating a new rectangle file on the hard disk i want it to be rectangle8.bmp

now it’s trying to create another rectangle1.bmp and give exception and i don’t want to delete the existing files but to create new ones.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

and make it as much as possible generic. but the main goal is to create new files names according to those existing and continue the counting.

>Solution :

You can write a method that checks if the proposed name exists or not

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;
}

and call it in this way:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading