Move files between folders with C #

1

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

Here is the method that accomplishes 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 installation files for this application are 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 a file that is being used (such as the application's own .exe) to the application directory, 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.

So I'd like some help with this problem:

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

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

If you can help with these DOS commands.

    
asked by anonymous 03.04.2014 / 18:35

1 answer

3

.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, enter the same file name:

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

EDIT I think I understand what you want to do:

If you are doing a type of automatic updater, I would do so:

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

  • In the main application, when you click refresh, 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 it automatically.

03.04.2014 / 18:52