I would use the .NET TempFileCollection class because it is built-in, available in older versions of .NET and implements the IDisposable interface and thus cleans up after itself if used, for example. combined with the keyword "using" .
Here is an example that extracts text from an embedded resource (added through the project properties pages โ the Resources tab, as described here: How to insert a text file into the .NET assembly ? , then set the value "EmbeddedResource" to the built-in properties of the file).
// Extracts the contents of the embedded file, writes them to a temp file, executes it, and cleans up automatically on exit. private void ExtractAndRunMyScript() { string vbsFilePath; // By default, TempFileCollection cleans up after itself. using (var tempFiles = new System.CodeDom.Compiler.TempFileCollection()) { vbsFilePath= tempFiles.AddExtension("vbs"); // Using IntelliSense will display the name, but it the file name // minus its extension. System.IO.File.WriteAllText(vbsFilePath, global::Instrumentation.Properties.Resources.MyEmbeddedFileNameWithoutExtension); RunMyScript(vbsFilePath); } System.Diagnostics.Debug.Assert(!File.Exists(vbsFilePath), @"Temp file """ + vbsFilePath+ @""" has not been deleted."); }
user3454591 Apr 04 '14 at 0:32 2014-04-04 00:32
source share