See if there is a file in a folder C # [closed]

-1

I wanted to verify that the file that the user chose already exists in a given folder at least. If it does not exist, you can copy the file.

    
asked by anonymous 30.05.2017 / 15:20

2 answers

2

Just copy the file to a folder and execute the one that was copied. I did not understand your code, but I did an example to try to help you.

private void AbrirArquivo()
{
        string arquivo = listView.SelectedItems[0].Text.ToString();
        string diretorio = @"C:\ByMe\SOLUTIONS\Dictation1\Database\Updates\";
        string diretorioBkp = @"C:\ByMe\SOLUTIONS\Dictation1\Database\Updates\Backup\";


        FileInfo file = new FileInfo(diretorio+arquivo);
        if (file.Exists)
        {
              FileInfo fBkp = new FileInfo(diretorioBkp + arquivo);
              if (fBkp.Exists)
              {
                  //se já existir uma cópia, deleto.
                  fBkp.Delete();
              }
              //faço a cópia do arquivo para a pasta bkp
              file.CopyTo(fBkp.FullName);
              //Abro o arquivo da pasta backup, o arquivo raiz, continua inalterado
              System.Diagnostics.Process.Start(fBkp.FullName);
        }
        else
        {
           MessageBox.Show("Arquivo selecionado não existe!");
        }
}
    
30.05.2017 / 15:34
2

To verify that the file exists, or to work (copy, read, create, and so on) with files, use the File :

File.Exists("Destino do arquivo")

So just get it if it does not exist with a negation operator ! and if it does not exist you make the copy: D

//Se diferente de verdadeiro  
if(!File.Exists("Destino do arquivo"))
{
//Cria o arquivo
File.Copy("Caminho do arquivo","Destino do arquivo");
}
    
30.05.2017 / 15:39