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();
}
} //...
}