Created
March 15, 2014 11:33
-
-
Save beratdogan/9565628 to your computer and use it in GitHub Desktop.
C#'ta Exe icine Gomulmus Baska Bir Exe'yi Calistirmak
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
string tempFile; | |
using (var tmp = new TempFile()) | |
{ | |
// Dosyanin calistirilabilir olmasi icin uzantisini .exe olarak degistiriyoruz. | |
tempFile = Path.ChangeExtension(tmp.Path, ".exe"); | |
// Burada gomdugumuz dosyayi, byte-byte okuyup temp dosyamizin icine yaziyoruz. | |
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("DOSYADINIZ.exe")) | |
{ | |
byte[] streamData = new byte[stream.Length]; | |
stream.Read(streamData, 0, streamData.Length); | |
File.WriteAllBytes(tempFile, streamData); | |
} | |
// Dosyamiz artik disarida olduguna gore, bir islem olusturup, onu calistiralim. | |
ProcessStartInfo startInfo = new ProcessStartInfo(); | |
startInfo.FileName = tempFile; | |
Process processTemp = new Process(); | |
processTemp.StartInfo = startInfo; | |
processTemp.EnableRaisingEvents = true; | |
try | |
{ | |
processTemp.Start(); | |
processTemp.WaitForExit(); // Program bitene kadar baska bir islem yapmayalim diye, bekliyoruz... | |
} | |
catch { } | |
} |
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
sealed class TempFile : IDisposable | |
{ | |
string path; | |
public TempFile() : this(System.IO.Path.GetTempFileName()) { } | |
public TempFile(string path) | |
{ | |
if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); | |
this.path = path; | |
} | |
public string Path | |
{ | |
get | |
{ | |
if (path == null) throw new ObjectDisposedException(GetType().Name); | |
return path; | |
} | |
} | |
~TempFile() { Dispose(false); } | |
public void Dispose() { Dispose(true); } | |
private void Dispose(bool disposing) | |
{ | |
if (disposing) | |
{ | |
GC.SuppressFinalize(this); | |
} | |
if (path != null) | |
{ | |
try { File.Delete(path); } | |
catch { } // best effort | |
path = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment