-1
The app saves photos of some parts here at the service, and we use a barcode reader that works as a keyboard, to read and save the data automatically, without the operator needing to use the mouse, but when two photos have to be taken from the same piece, generates this disorder for the operator, where he has to renown the file at the time of overwriting not to overwrite.
It is possible to save files with the same name in C#, how can I avoid conflict? Or make the files rename automatically, without the user having to do it manually.
I tried to finish that code:
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");
However I did not succeed, my code to save 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();
}
} //...
}
I didn’t understand very well, you can’t save files with the same name and extension in the same directory...
– Leandro Angelo
In reality what this command does is put a comment with a different time than the first one that was saved, so it saves both files or more with the same name, but I’m not able to make it work
– Felipe Deolindo
It doesn’t have to be the same name, only if at the time you were going to use the application to save, it didn’t appear to rename, you did it automatically, like example - example[1] - example[2] it would already help me
– Felipe Deolindo
In this case the most common practice is to concatenate the date the end of the file name, Example teste_20180409100836.txt
– Leandro Angelo
Another thing you can do is rename the old file by adding its name change date and leave the current always with the fixed name
– Leandro Angelo
then it is that in the case the application it saves photos of some parts here in the service, then it uses a barcode reader that works like keyboard, making this saving become automatic, without it need to be using the mouse, but when two photos have to be taken of the same piece, it generates this disorder for the operator, where he has to renown the file at the time of overwrite and use the mouse to finish saving, so I wanted to automate this
– Felipe Deolindo
It would just fix the conceptual error of using the read as the file name, or simply add an incremental suffix.
– Bacco