See if there is a file in a folder c#

Asked

Viewed 3,189 times

-1

I wanted you to check if the file you chose already exists in a certain folder defined at least. If it does not exist, you can copy the file.

  • I was a little confused, your title asks one thing and the other question... Do you want to see if it exists or create a copy? edit your question to make it clearer

  • Brazilian programmers; write the functional requirement and ask for technical help. From what I can understand, she wants to make a backup of the file that will be selected, and open in this new directory. However the chunk of code still barely does the task, so I think she wants a complete method that does this.

  • I fixed it. My problem even @Andrewpaes, is that I don’t really know how to do that part of checking for the file.

  • Well, now we’re getting to the bottom of it. The answer below is already half the way, however, you want to check only the existence of the file, or compare them too? By date? by content? per number of bytes? There are many possible checks for a file. I believe that checking the existence with Fileexists() is paramount, then you can check last update date, and then amount of bytes. if it is text file, it could still make a "diff", but this is much more elaborate.

  • I still can’t understand what needs to be done, even though I walked a path

  • I closed it because the answers seem to be kicks of what you’re looking for, and the fact that they’re opposed and they’re both getting votes feels like it’s all meaningless here, it’s only going to hurt other people who read it here. I’ll reopen when I can understand what you need.

  • I’ve done it. Thank you all and forgive my confusion.

Show 2 more comments

2 answers

3


Just make a copy of the file to a folder and run this one that was copied. I didn’t understand your code, but I made 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!");
        }
}

2

To check if the file exists, or work (copy, read, create and etc) with files, use the class File:

File.Exists("Destino do arquivo")

So just get in if the same doesn’t exist with a denial operator "!" and if it doesn’t 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");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.