Save files with the same name

-1

The application saves photos of some parts here in the service, and we use a barcode reader that works as a keyboard, to read and save the data automatically, without the operator having to use the mouse, however when two photos have to to be taken from the same piece, generates this pro-operator disorder, where it has to rename the file at the time of overwrite not to overwrite.

Is it possible to save files with the same name in C #, how can I avoid conflict? Or make the files rename automatically, without requiring the user to do it manually.

I tried to use this link:

if (!Directory.Exists(""))
{
    Directory.CreateDirectory(@"c:\text");
}

string FileName = System.IO.Path.Combine(@"c:\text", DateTime.Now.ToString("yyy-MM-dd-hh-mm-ss"));
File.Create(FileName + ".txt");

But I did not succeed, my code for saving images is this one:

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        //abre a opção de salvar como, para selecionar a pasta
        SaveFileDialog saveFileDialog1 = new SaveFileDialog
        {
            //InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures),
            Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif",
            Title = "Salvar o arquivo de imagem",
            RestoreDirectory = true

        };
        saveFileDialog1.ShowDialog();

        {
            if (!Directory.Exists(""))
            {
                Directory.CreateDirectory(@"c:\text");
            }

            string FileName = System.IO.Path.Combine(@"c:\text", DateTime.Now.ToString("yyy-MM-dd-hh-mm-ss"));
            File.Create(FileName + ".txt");

        }

        // 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.pictureBoxScreenshot.Image.Save(fs,
                       System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;

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

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

            fs.Close();
        }
    } //...
}
    
asked by anonymous 09.04.2018 / 14:41

1 answer

0

For those who have the same problem one day as mine, I was able to solve the case like this:

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        //abre a opção de salvar como, para selecionar a pasta

        SaveFileDialog saveFileDialog1 = new SaveFileDialog

        {
            //InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures),

            Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif",
            Title = "Salvar o arquivo de imagem",
            RestoreDirectory = true,
         };

        saveFileDialog1.OverwritePrompt = false;
        saveFileDialog1.ShowDialog();


        // se o nome do arquivo não for vazio, abre para salvar
        if (saveFileDialog1.FileName != "")
        {
            int fileCount = 0;
            string fullPath = saveFileDialog1.FileName;
            string fileNamewithoutExt = Path.GetFileNameWithoutExtension(fullPath);
            string dirPath = Path.GetDirectoryName(fullPath);
            string ext = Path.GetExtension(fullPath);
            string newPath = string.Empty;

            while (File.Exists(newPath = Path.Combine(dirPath, fileNamewithoutExt + (fileCount > 0 ? "[" + fileCount + "]" : "") + ext)))
            { fileCount++; }

            // Salva a imagem no formato certo
            switch (saveFileDialog1.FilterIndex)
            {
                case 1:
                    this.pictureBoxScreenshot.Image.Save(newPath,
                       System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;

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

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

        }

    }
    catch (Exception ex)
    {
        MessageBox.Show("Erro " + ex.Message);
    }
}
    
09.04.2018 / 20:40