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");
}