C#-Programm kopiert sich selbst und wird am neuen Speicherort ausgeführtC#

Ein Treffpunkt für C#-Programmierer
Guest
 C#-Programm kopiert sich selbst und wird am neuen Speicherort ausgeführt

Post by Guest »

Das ist also Teil eines Programms, das ich schreibe. Ich möchte, dass sich das Programm an einen bestimmten Speicherort kopiert, eine Batchdatei schreibt und ausführt, die den alten Prozess beendet und den neuen ausführt. Das Problem besteht darin, dass dadurch eine Endlosschleife des ursprünglichen Programms ausgeführt und beendet wird, anstatt der neuen Version. Hier ist der Code, den ich verwende.

Code: Select all

string placementDirectory = Environment.GetFolderPath("Where I want to place the file");
string folderName = "KL";
string currentProccessDirectory = Environment.CurrentDirectory;
string currentProccessName = Process.GetCurrentProcess().ProcessName + ".exe";

if (currentProccessDirectory != placementDirectory + "\\" + folderName) //This is the check to see if it is in the correct directory
{
if (Directory.GetDirectories(placementDirectory).Contains(placementDirectory + "\\" + folderName)) // This checks if the placement directory exists
{
if (Directory.GetFiles(placementDirectory + "\\" + folderName).Contains(placementDirectory + "\\" + folderName + "\\" + currentProccessName))
{
System.IO.File.Delete(placementDirectory + "\\" + folderName + "\\" + currentProccessName); // if the file already exists at this location, it deletes i
}
}
else // if the Placement directory does not exist, it creates it
{
Directory.CreateDirectory(placementDirectory + "\\" + folderName);
}
System.IO.File.Copy(currentProccessDirectory + "\\" + currentProccessName, placementDirectory + "\\" + folderName + "\\" + currentProccessName); //Copies the running proccess to the desired location
using (StreamWriter canWrite = new StreamWriter(placementDirectory + "\\" + folderName + "\\Copier.bat")) // Creation of the batch file to kill the and run the new proccess
{
canWrite.WriteLine("taskkill /f /im \"" + currentProccessName + "\"");
canWrite.Write("START \"" + placementDirectory + "\\" + folderName + "\" " + "\"" + currentProccessName + "\"");
canWrite.Flush();
}
Process.Start(placementDirectory + "\\" + folderName + "\\Copier.bat");
}
Aus irgendeinem Grund öffnet die Batchdatei immer die ursprüngliche ausführbare Datei, obwohl sie angewiesen wird, die neue zu öffnen. Und ich habe keine Ahnung warum.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post