Daily folder creation in C#

Asked

Viewed 58 times

1

I have two programs distinct from each other here, one of them creates folders named with the current date whenever I run it, then I made a Schedule for it and daily it runs alone, and the other is to capture webcam images , the webcam is to open the directory where what creates folders is configured, but the user must select the latest folder to save the necessary photos of that day, what I wanted to do was to unify both, taking out of the hand of the operator the need to select the folder.

Example: The Webcam Program creates whenever you press save as a most recent folder according to the day and if the folder already exists appeals open it to type the name and save.

I’ve looked in several places, some say it’s not possible and others claim it can be done yes, I wonder if anyone has ever done something like this or knows at least a way for me to start, I will leave the current code of the webcam program to save the images and the code of what creates folders.

private void btnSalvar_Click(object sender, System.EventArgs e)
{
    try
    {
        //abre a opção de salvar como, para selecionar a pasta
        SaveFileDialog saveFileDialog1 = new SaveFileDialog
        {
            Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif",
            Title = "Salvar o arquivo de imagem",
            InitialDirectory = @"\\MI7627\Imagens"
        };
        saveFileDialog1.ShowDialog();

        // se o nome do arquivo não for vazio, abre para salvar
        if (saveFileDialog1.FileName != "")
        {
            // salva a imagem por fileStream
            System.IO.FileStream fs =
               (System.IO.FileStream)saveFileDialog1.OpenFile();
            // Salva a imagem no formato certo
            switch (saveFileDialog1.FilterIndex)
            {
                case 1:
                    this.picImagem.Image.Save(fs,
                       System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;

                case 2:
                    this.picImagem.Image.Save(fs,
                       System.Drawing.Imaging.ImageFormat.Bmp);
                    break;

                case 3:
                    this.picImagem.Image.Save(fs,
                       System.Drawing.Imaging.ImageFormat.Gif);
                    break;
            }

            fs.Close();
        }

    }

Folder maker:

using System;
using static System.Console;
using System.IO;

public class CreateFileOrFolder
{
    public static void Main()
    {
        Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); // C:\ProgramData no Win7
        var activeDir = (@"\\MI7627\Imagens"); //caminho onde salva as fotos
        string newPath = Path.Combine(activeDir, DateTime.Now.ToString("yyyy-MM-dd")); //subpasta onde cria de acordo com a data
        Directory.CreateDirectory(newPath); // cria o diretório
        newPath = Path.Combine(newPath, Path.GetRandomFileName()); 
        if (!File.Exists(newPath));

    }

    void CriarPasta(string Path)//Cria a função/void
    {
        if (Directory.Exists(Path))//Verifica se já existe uma pasta com o mesmo *path*
        {
            for (int i = 0; !Directory.Exists((Path + "(" + i + ")")); i++)//Verifica se exste uma pasta com o nome + (i)
            {
                Directory.CreateDirectory(Path + "(" + i + ")");//Cria a pasta
            }
        }
        else// se não
            Directory.CreateDirectory(Path);//Cria a pasta
    }

}

1 answer

1


You can use the Getlastwritetime to get the latest directory: Ex:

new DirectoryInfo(path).GetDirectories().OrderByDescending(d=>d.LastWriteTimeUtc).First();
  • very good, did not know this function, thanks man

Browser other questions tagged

You are not signed in. Login or sign up in order to post.