Error creating file "Can not create a file when that file already exists"

1

I'm trying to create a file from a call from a web service.

When I run it I get the error Cannot create a file when that file already exist

According to the debug, the code falls on the line File.Move(originalFileName, newFileName); Here is my code, could you tell me what I'm doing wrong?

Thank you very much

string testExport = proj.ProjectTitle; ;
string outCsvFile = string.Format(@"D:\testExport\{0}_.zip", testExport); 
byte[] MyBinFiles = db.GetResponse(AllProjectIds);

DirectoryName = Path.GetDirectoryName(outCsvFile);

String finalPath = Path.Combine(Path.GetDirectoryName(outCsvFile),
               Path.GetFileNameWithoutExtension(outCsvFile)
              + DateTime.Now.ToString("_yyyyMMdd HHmms")
              + Path.GetExtension(outCsvFile));


Console.WriteLine("Create ZIP: " + finalPath);

System.IO.File.WriteAllBytes(finalPath, MyBinFiles);

Thread.Sleep(1000);
Console.WriteLine("WriteAllBytes :  " + finalPath);


string zipFileName = Path.GetFileNameWithoutExtension(outCsvFile)
              + DateTime.Now.ToString("_yyyyMMdd HHmms");


while (!FileExtract(finalPath, Path.GetDirectoryName(outCsvFile)))
{
    Thread.Sleep(1000);
}

while (!FileDelete(finalPath))
{
    Thread.Sleep(1000);
}

//Thread.Sleep(1000);

DirectoryInfo d = new DirectoryInfo(Path.GetDirectoryName(outCsvFile));


foreach (FileInfo file in d.GetFiles("*.csv"))
{
    if (!(file.Name.IndexOf("__") >= 0))
    {
        string originalFileName = file.FullName;
        int y = file.Name.IndexOf("_");
        string newFileName = zipFileName + file.Name.Substring(y);
        newFileName = Path.GetDirectoryName(outCsvFile) + @"\" + newFileName;
        File.Move(originalFileName, newFileName);

        files.Add(newFileName);
    }

}
string zipFolder = ConfigurationSettings.AppSettings["ZipFolder"].ToString();

//ZipAllFiles
while (!ZipAllFiles(DirectoryName, zipFolder))
{
  hread.Sleep(1000);
}

onsole.WriteLine("Deleting All Csv file");
    
asked by A arancibia 24.03.2016 в 21:50
source

2 answers

1

If we assume that "there really is not a file with the same name", in your case it seems to me that when doing Substring() in this line:

string newFileName = zipFileName + file.Name.Substring(y);

You are not getting a file path, maybe something that looks like a directory:

"c:\datos\mi_archivo_nuevo"

Make sure the file path is obtained for variable newFileName

"c:\datos\mi_archivo_nuevo.csv"

this, so you can perform:

  File.Move(@"c:\datos\mi_archivo_original.csv", @"c:\datos\mi_archivo_nuevo.csv");
  • Another thing that may be happening is that the folder where you want to move the file does not exist!.
answered by 24.03.2016 в 22:18
0

If a file with the same name already exists, or you delete the file first and then move it or replace it (generating a backup copy of the old file).

To remove it before moving:

using System.IO;

...
...

if (File.Exists (archivoDestino_Path))  
   File.Delete (archivoDestino_Path);

File.Move (archivoFuente_Path, archivoDestino_Path);

...

But for what you say, the file was previously generated. So, if we want to replace Destiny_Path file, and save a backup of the old Destiny_Path file:

 Using System.IO;

 ...
 ...

 File.Replace (archivoFuente_Path, archivoDestino_Path, archivoBackUp_Path);

 ...
    
answered by 17.07.2016 в 11:40