Esempio n. 1
0
 public HTempDirectory
     (string tempbase
     , string copyto   // [=null]
     )
 {
     this.initpath = HEnvironment.CurrentDirectory;
     this.dirinfo  = HDirectory.CreateTempDirectory(tempbase);
     this.copyto   = copyto;
 }
Esempio n. 2
0
        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);
        }