yair shalvi
yair shalvi

Reputation: 89

How can i make sure that if i closed the program and run again it will create the new file in a new directory?

I have this method:

private void CreateAnimatedGif(bool ToCreate)
{
    string combined = null;
    DirectoryInfo di = new DirectoryInfo(sf);
    FileInfo[] fi = di.GetFiles("*.gif");
    if (simtest == fi.Length - 1)
    {
        simtest = 0;
    }

    if (AnimatedGifFiles.Count == 0)
        startTime = DateTime.Now;

    if (ToCreate == false)
    {
        combined = Path.Combine(sf, fi[simtest].FullName);
        AnimatedGifFiles.Add(combined);//last_file);
        simtest += 1;
    }
    else
    {
        if (AnimatedGifFiles.Count > 1)
        {
            rainEventCounter += 1;
            AnimatedGifDirectoryEvent = "Rain event " + rainEventCounter.ToString();
            string eventDir = Path.Combine(AnimatedGifDirectory, AnimatedGifDirectoryEvent);

            if (!Directory.Exists(eventDir))
            {
                Directory.CreateDirectory(eventDir);
            }

            string outputFile = System.IO.Path.Combine(
                eventDir,
                string.Format(
                    System.Globalization.CultureInfo.InvariantCulture,
                    "Event-{0:yyyy-MM-dd-HHmmss}_{1:yyyy-MM-dd-HHmmss}.gif", startTime, DateTime.Now
                )
            );

            unfreezWrapper1.MakeGIF(AnimatedGifFiles, outputFile, 80, true);
        }

        AnimatedGifFiles.Clear();
    }
}

In this part i'm creating the directory each time to create inside the outputFile.

rainEventCounter += 1;
AnimatedGifDirectoryEvent = "Rain event " + rainEventCounter.ToString();
string eventDir = Path.Combine(AnimatedGifDirectory, AnimatedGifDirectoryEvent);
if (!Directory.Exists(eventDir))
{
    Directory.CreateDirectory(eventDir);
}

The problem is when i'm running the program over again then rainEventCounter is 0 so it will put the new outputFile in the first existing directory. What i want it to do is to check if there are any directories in AnimatedGifDirectory:

OK edit:

I added now to the top of the class a variable string[] dirs And in the class constructor i did:

dirs = Directory.GetDirectories(AnimatedGifDirectory);

So now when i'm running my program there are 0 sub directories. But then i run the program over again and there are for example 3 directories. Now dirs contain 3 directories.

Now i want that next time AnimatedGifDirectoryEvent will be "Rain event 4" as sub directory name and not "Rain event 1" again.

Of course if dirs length is 0 then AnimatedGifDirectoryEvent will be "Rain event 1".

Upvotes: 0

Views: 68

Answers (2)

Habib
Habib

Reputation: 223332

You have multiple ways to get it. First you can get the Length of your Directory array and then return that. Like:

public static int GetNextNumber(string directoryPath)
{
    var dirs = Directory.GetDirectories(directoryPath);
    return dirs.Length + 1;
}

Or you can parse the digit from your directory name to int, get the Max from that and return that + 1.

public static int GetNextNumberWithParsing(string directoryPath)
{
    var dirs = Directory.GetDirectories(directoryPath);
    var number = dirs.Select(r => int.Parse(r.Replace("Rain event ", ""))).Max();
    return number + 1;
}

and then call it in your application like:

rainEventCounter = GetNextNumberWithParsing(AnimatedGifDirectory);

The only problem in using first method is if your directories get deleted (may be due to some cleanup job), then Length could return an invalid directory name. Consider if you have directories like:

Raise event 1
Raise event 2
Raise event 3
Raise event 4

Then if you use dir.Length it would return 4 and then you can use dir.Length + 1 for next name.

But if directories 1 and 2 gets deleted in that case you will get a Length of 2 and your next directory number will be 3, and now it will throw an error since a directory with that name already exists.

Upvotes: 3

Dave Zych
Dave Zych

Reputation: 21897

You have the count of directories in your root directory in the dirs variable. Use dirs.Length + 1 to set the new directory name.

AnimatedGifDirectoryEvent = "Rain event " + (dirs.Length + 1);

Upvotes: 2

Related Questions