Last active
May 14, 2020 16:21
-
-
Save joecastelo/e6e13b25b94c7419ef340205f17c9e20 to your computer and use it in GitHub Desktop.
Logic to Copy and Paste all files in a folder to another subfolder where the file you are launching the executable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Security; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace ExecutableLogic | |
{ | |
class RunStandalone | |
{ | |
public string Args { get; set; } | |
public RunStandalone(string args) | |
{ | |
Args = args; | |
} | |
public void RunProgram(string filename) | |
{ | |
try | |
{ | |
var tmpPath = Path.GetTempPath(); | |
var randomfolder = Path.GetRandomFileName().Split('.')[0]; | |
var extension = Path.GetExtension(filename); | |
var dest = Path.Combine(tmpPath, randomfolder); | |
//MessageBox.Show(dest); | |
var fileonTmp = Path.Combine(dest, Path.GetFileNameWithoutExtension(filename) + extension); | |
//MessageBox.Show(fileonTmp); | |
try | |
{ | |
Copy(Path.GetDirectoryName(filename), dest); | |
//MessageBox.Show(string.Join("\n", contextArgs.ToArgs())); | |
System.Diagnostics.Process.Start(fileonTmp, Args); | |
} | |
catch (Exception e) | |
{ | |
throw new Exception("Did not work" + e.Message); | |
} | |
} | |
catch (SecurityException ex) | |
{ | |
throw new Exception($"Security error.\n\nError message: {ex.Message}\n\n" + | |
$"Details:\n\n{ex.StackTrace}"); | |
} | |
} | |
public static void Copy(string sourceDirectory, string targetDirectory) | |
{ | |
var diSource = new DirectoryInfo(sourceDirectory); | |
var diTarget = new DirectoryInfo(targetDirectory); | |
CopyAll(diSource, diTarget); | |
} | |
public static void CopyAll(DirectoryInfo source, DirectoryInfo target) | |
{ | |
Directory.CreateDirectory(target.FullName); | |
// Copy each file into the new directory. | |
foreach (FileInfo fi in source.GetFiles()) | |
{ | |
Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name); | |
fi.CopyTo(Path.Combine(target.FullName, fi.Name), true); | |
} | |
// Copy each subdirectory using recursion. | |
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) | |
{ | |
DirectoryInfo nextTargetSubDir = | |
target.CreateSubdirectory(diSourceSubDir.Name); | |
CopyAll(diSourceSubDir, nextTargetSubDir); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment