Move files between folders with C#

Asked

Viewed 2,741 times

1

I have an application that checks and downloads a file as soon as it runs through the Window_Loaded.

Here is the method that performs this task:

        XmlDocument doc = new XmlDocument();
        doc.Load("http://www.meusite.com.br/dirdaaplicacao/arquivoXML.xml");

        XmlNode node = doc.DocumentElement.SelectSingleNode("/Application/Version");
        XmlNode node1 = doc.DocumentElement.SelectSingleNode("/Application/ZipFile");
        string version = node.InnerText;
        string zipfile = node1.InnerText;
        string End = (@"\\servidor\wwwroot\meusite.com.br\dirdaaplicacao\");
        string file = (End + zipfile);
        string versionAssembly = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

        if (Convert.ToDouble(version) <= Convert.ToDouble(versionAssembly))
        {
            MessageBox.Show("Sistema Atualizado " + version);
        }
        else
        {
            ZipFile zipFile = ZipFile.Read(file);
            {
                foreach (ZipEntry zipEntry in zipFile)
                {
                    zipEntry.Extract(@"C:\IASD\Diretorio\Temp", ExtractExistingFileAction.OverwriteSilently);
                }
            }
            MessageBox.Show("Atualizando o sistema! A aplicação será reiniciada! Versão: " + version);

The executable and the installation files of this application are in c:\IASD\Diretorio and the files that are uncompressed will be in c:\IASD\Diretorio\Temp.

I created the Temp folder because it is not possible to download to the application directory a file that is being used (such as the application’s own .exe) even using the Ionic.zip API.

zipEntry.Extract(@"C:\IASD\Diretorio", ExtractExistingFileAction.OverwriteSilently);

The error that is reported refers to The file 'nome_do_arquivo' already exists.

Then I would like a help to this problem:

At the end of the extract to the Temp folder I call a DOS command which closes the application, moves the files from the folder c:\IASD\Diretorio\Temp to the folder c:\IASD\Diretorio and restarts the application.

Note: I tested the above method running directly from Visual Studio and it worked perfectly.

If you can help with these commands in DOS.

1 answer

3


The . Net has a specific method for moving files:

System.IO.File.Move("c:\\origem.txt", "c:\\diretorio\\destino.txt");

Note that this method is able to move and also rename the file... to move only, indicate the same filename:

System.IO.File.Move("c:\\origem.txt", "c:\\diretorio\\origem.txt");

EDIT I think I understand what you want to do:

If you’re doing some kind of automatic updater, I’d do it like this:

You will have to make two applications then, a main one, and another that will be the updater.

  • In the main application, when clicking update, the main application should update the updater, then open the updater and then close.

  • The updater in turn updates the main application, starts the main application and then closes, all of this automatically.

  • How do I move a file that is running with the application open? I thought in a cmd command that as soon as the file is downloaded in temp folder, it would close the application, copy the files and then restart the application.

Browser other questions tagged

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