public HTempDirectory (string tempbase , string copyto // [=null] ) { this.initpath = HEnvironment.CurrentDirectory; this.dirinfo = HDirectory.CreateTempDirectory(tempbase); this.copyto = copyto; }
public static DirectoryInfo CreateTempDirectory(string tempbase = null) { while (true) { string path = "$" + System.IO.Path.GetRandomFileName(); if (System.IO.File.Exists(tempbase + path) == false) { return(HDirectory.CreateDirectory(tempbase + path)); } } }
public static string ParsePath(string value) { if (value.EndsWith("\\") == false) { value += "\\"; } if (HDirectory.Exists(value)) { return(value); } throw new Exception(); }
public void Dispose() { QuitTemp(); try { if (copyto != null) { HDirectory.DirectoryCopy(dirinfo.FullName, copyto, true); } dirinfo.Delete(true); } catch (Exception) { } }
public static int StartAsBatchSilent(Tuple <string, IList <string>, IList <string> > env, List <string> lineStdout, List <string> lineStderr, params string[] commands) { string tempbase = null; IList <string> filesIn = null; IList <string> filesOut = null; if (env != null) { tempbase = env.Item1; HDebug.Assert(tempbase != null); HDebug.Assert(HDirectory.Exists(tempbase)); filesIn = env.Item2; filesOut = env.Item3; } string currdir = HEnvironment.CurrentDirectory; if (tempbase != null) { var dirinfo = HDirectory.CreateTempDirectory(tempbase); HEnvironment.CurrentDirectory = dirinfo.FullName; } if (filesIn != null) { foreach (string file in filesIn) { HFile.Copy(currdir + "\\" + file, file); } } int exitcode; { string tmpbatpath = HFile.GetTempPath("bat"); HFile.WriteAllLines(tmpbatpath, commands); { if (lineStderr == null) { lineStderr = new List <string>(); // collect into garbage storage if they are initially null. } if (lineStdout == null) { lineStdout = new List <string>(); // collect into garbage storage if they are initially null. } // capture error message in console System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = tmpbatpath; proc.StartInfo.RedirectStandardError = (lineStderr != null); proc.StartInfo.RedirectStandardOutput = (lineStdout != null); proc.StartInfo.UseShellExecute = false; proc.Start(); if (lineStderr != null) { lineStderr.Clear(); lineStderr.AddRange(proc.StandardError.ReadToEnd().Replace("\r", "").Split('\n')); } if (lineStdout != null) { lineStdout.Clear(); lineStdout.AddRange(proc.StandardOutput.ReadToEnd().Replace("\r", "").Split('\n')); } proc.WaitForExit(); exitcode = proc.ExitCode; } Thread.Sleep(100); HFile.Delete(tmpbatpath); } if (filesOut != null) { foreach (string file in filesOut) { if (HFile.Exists(file)) { HFile.Copy(file, currdir + "\\" + file); } } } if (HEnvironment.CurrentDirectory != currdir) { string delpath = HEnvironment.CurrentDirectory; HEnvironment.CurrentDirectory = currdir; try{ HDirectory.Delete(delpath, true); } catch (System.IO.IOException) { } } return(exitcode); }