Back-up_Bats_Daemond Process 对小部分bats的守护进程
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media;
using static System.Console;
namespace copybat_daemon_process
{
class Program
{
static void Main(string[] args)
{
#region
Console.Title = "Robcopybat_Daemon_Process_Prcess";
string startmode;
judge_module: Write("Begin \\ Write [y \\ n]: ");
startmode = Convert.ToString(ReadLine());
if (startmode == "y")
{
revise_file();
goto start_module;
}
else if(startmode == "n")
{
System.Diagnostics.Process.Start(@"E:\Ballow_OS\copy_tab_lib.txt");
goto judge_module;
}
else
goto judge_module;
start_module: do
{
Write("[$ Fast light $]: ");
string aa = Convert.ToString(ReadLine());
if (aa == "q")
{
WriteLine("-->k: shutdown process\n" +
"-->add: add new process\n" +
"-->ss: shut console window and shutdown all process\n" +
"-->ls: show all latest process name\n" +
"-->o: revise copy_tab_lib.txt\n" +
"-->up: update process");
}
else if(aa == "o")
{
System.Diagnostics.Process.Start(@"E:\Ballow_OS\copy_tab_lib.txt");
}
else if(aa == "up")
{
revise_file();
}
else if (aa == "add")
{
string line3, line4, isoverwrite;
Write("add process name: ");
line3 = ReadLine();
Write("add process bat path: ");
line4 = ReadLine();
overwrite_module: Write("this process can be overwirted copt_tab_lib.txt [ y / n ]: ");
isoverwrite = Convert.ToString(ReadLine());
if (isoverwrite != "y" && isoverwrite != "n")
goto overwrite_module;
function.createtask(line3, @line4, false, false);
if (isoverwrite == "y")
{
var file = new FileStream(@"E:\Ballow_OS\copy_tab_lib.txt", FileMode.Append);
var writer = new StreamWriter(file);
WriteLine();
writer.WriteLine(line3);
writer.WriteLine(line4);
writer.Close();
file.Close();
}
}
else if (aa == "ss")
{
function.killall();
break;
}
else if (aa == "kall")
{
function.killall();
}
else if (aa == "k")
{
Write("the index of killed process is: ");
int index = Convert.ToInt32(ReadLine());
function.killprocess(index);
}
else if (aa == "ls")
{
function.showtask();
}
else
;
} while (true);
WriteLine("Ending");
#endregion
}
private static void revise_file()
{
var file = new System.IO.StreamReader(@"E:\Ballow_OS\copy_tab_lib.txt");
string line1 = "";
while ((line1 = file.ReadLine()) != null)
{
string line2 = file.ReadLine();
function.createtask(line1, @line2, false, false);
}
file.Close();
}
}
public static class function
{
private static List<Process> processlist = new List<Process>();
private static List<string> processnamelist = new List<string>();
public static void killall()
{
if(processlist.Count() == 0 && processnamelist.Count() == 0)
return;
for(int i = 1; i <= processlist.Count(); ++i)
{
processlist[i - 1].Kill();
}
processlist.Clear();
processnamelist.Clear();
}
public static void showtask()
{
if (processlist.Count() == 0)
{
WriteLine("process list is empty");
}
for(int i = 1; i <= processlist.Count(); ++i)
{
WriteLine("[ " + i + " ] " + "----> " + processnamelist[i - 1]);
}
}
public static void createtask(string filename, string path, bool showWindow, bool waitForExit)
{
if(processnamelist.Count() == 0)
{
addprocess(filename, path);
return;
}
for(int i = 1; i <= processnamelist.Count(); ++i)
{
if (processnamelist[i - 1] == filename)
return;
}
addprocess(filename, path);
}
private static void addprocess(string filename, string path)
{
Process p = new Process();
ProcessStartInfo si = new ProcessStartInfo();
processlist.Add(p);
processnamelist.Add(filename);
RunCmd(p, si, filename, path, false, false);
}
public static bool killprocess(int index)
{
if(index > processlist.Count())
{
WriteLine("Num is error");
return false;
}
WriteLine("[ " + processnamelist[index - 1] + " ]process have been killed.");
processlist[index - 1].Kill();
processlist.RemoveAt(index - 1);
processnamelist.RemoveAt(index - 1);
return true;
}
private static void RunCmd(Process p, ProcessStartInfo si, string filename, string path, bool showWindow, bool waitForExit)
{
si.FileName = path;
path = "cmd -c " + path;
si.Arguments = path;
si.UseShellExecute = false;
si.CreateNoWindow = showWindow;
si.RedirectStandardOutput = true;
si.RedirectStandardError = true;
p.StartInfo = si;
p.Start();
//WriteLine(filename + "is Runing");
}
}
}