Code Source Main WTS 17-09-13
Code Source Main WTS 17-09-13
System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; System.IO; System.Threading; System.Net; System.Net.NetworkInformation; System.Web; Microsoft.Win32; System.Diagnostics;
namespace WTS { public partial class WTSHome : Form { #region GENERAL public public public public struct Ping monPing = new Ping(); Thread fluxEnvoi; Thread fluxReception; string scanFile; Ordi { public string nomOrdi; public string ipOrdi;
} private List<Ordi> hostList = new List<Ordi>(); private string useFile; private string scriptFile; public WTSHome() { InitializeComponent(); fluxEnvoi = new Thread(envoi); fluxReception = new Thread(reception); //scanbtnNetwork.Enabled = true; //scanbtnLaunch.Enabled = false; } private void WTSHome_Load(object sender, EventArgs e) { verificationDossier(); } private void WTSHome_FormClosing(object sender, FormClosingEventArgs e) { fluxEnvoi.Abort(); fluxReception.Abort(); } private void verificationDossier() { if ( Directory.Exists(Environment.CurrentDirectory + @"\log") ) { } else { System.IO.Directory.CreateDirectory(Environment.CurrentDirectory + @"\log"); } { } if ( Directory.Exists(Environment.CurrentDirectory + @"\log\temp") )
else { System.IO.Directory.CreateDirectory(Environment.CurrentDirectory + @"\log\temp"); } if (Directory.Exists(Environment.CurrentDirectory + @"\journal")) { } else { System.IO.Directory.CreateDirectory(Environment.CurrentDirectory + @"\journal"); } if ( File.Exists(Environment.CurrentDirectory + @"\log\temp\fileAddress.txt") ) { System.IO.File.Delete(Environment.CurrentDirectory + @"\log\temp\fileAddress.txt"); }
} private string creationScanTemp() { string fileScan = Environment.CurrentDirectory + @"\log\temp\scan" + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".txt"; System.IO.StreamWriter sw0 = File.CreateText(fileScan); sw0.Close(); System.IO.StreamWriter sw1 = File.CreateText(Environment.CurrentDirectory + @"\log\temp\fileAddress.txt"); sw1.Write(fileScan); sw1.Close(); return fileScan; } private void onglet_Selected(object sender, TabControlEventArgs e) { } #endregion #region PING /* RECHERCHE RESEAU */ private void scanbtnNetwork_Click(object sender, EventArgs e) { List<RadioButton> rdbs = new List<RadioButton>(); rdbs = quelreseau(); foreach ( RadioButton r in rdbs ) { scancheckNetwork.Controls.Add(r); } testIPinput(); } private List<RadioButton> quelreseau() { IPAddress[] hostIPs = Dns.GetHostAddresses(Dns.GetHostName()); string[] ipsall = new String[hostIPs.Length]; RadioButton rdb = new RadioButton(); List<RadioButton> rdbs = new List<RadioButton>(); for ( int i = 0; i < hostIPs.Length; i++ ) { ipsall[i] = hostIPs[i].ToString(); } for ( int o = 0; o < ipsall.Length; o++ ) { if ( ipsall[o].Contains('.') ) { rdb.Text = decoupe123(ipsall[o]); rdb.ForeColor = Color.Black; rdbs.Add(rdb); } if ( rdbs.Count == 1 ) { rdbs[0].Checked = true; int i = rdbs[0].Text.LastIndexOf('.'); scantxtIPnetwork.Text = rdbs[0].Text.Remove(i + 1); scantxtIPnetworkbis.Text = rdbs[0].Text.Remove(i + 1); } } return rdbs; } private string decoupe123(string s) { string[] stt = s.Split('.'); string st = stt[0] + "." + stt[1] + "." + stt[2] + ".0"; return st; } private void scancheckNetwork_Click(object sender, EventArgs e) { foreach ( RadioButton rb in scancheckNetwork.Controls.OfType<RadioButton>() ) { if ( rb.Checked ) {
scantxtIPnetwork.Text = rb.Text.Remove(( rb.Text.LastIndexOf('.') + 1 )); scantxtIPnetworkbis.Text = scantxtIPnetwork.Text; testIPinput(); } } } /* SAISIE IP */ private void scantxtIPmin_TextChanged(object sender, EventArgs e) { testIPinput(); } private void scantxtIPmax_TextChanged(object sender, EventArgs e) { testIPinput(); } private void testIPinput() { try { if ( int.Parse(scantxtIPmin.Text) > 0 && int.Parse(scantxtIPmin.Text) < int.Parse(scantxtIPmax.Text) && int.Parse(scantxtIPmax.Text) < 255 && scantxtIPnetwork.Text != "" ) { scanbtnLaunch.Enabled = true; } } catch { } } /* ENVOI DU PING */ private void scanbtnLaunch_Click(object sender, EventArgs e) { ping(); scantxtIPmin.Enabled = false; scantxtIPmax.Enabled = false; scanbtnNetwork.Enabled = false; scanbtnLaunch.Enabled = false; scancheckNetwork.Enabled = false; scanFile = creationScanTemp(); hostList.Clear(); scancheckHosts.Rows.Clear(); scancheckHosts.Update(); scancheckHosts.Enabled = true; } private void ping() { fluxEnvoi.Start(); fluxReception.Start(); } public void envoi() { Invoke((MethodInvoker)delegate { scanpbar.Minimum = 0; scanpbar.Maximum = int.Parse(scantxtIPmax.Text) int.Parse(scantxtIPmin.Text) + 1; scanpbar.Value = scanpbar.Minimum; }); for ( int i = int.Parse(scantxtIPmin.Text); i <= int.Parse(scantxtIPmax.Text); i++ ) { try { monPing.SendAsync(scantxtIPnetwork.Text + i.ToString(), 2500); Thread.Sleep(1500); } catch { i--; } } }
/* RETOUR DU PING */ private void reception() { monPing.PingCompleted += new PingCompletedEventHandler(retourPingEvent); } private void retourPingEvent(object sender, PingCompletedEventArgs e) { PingReply reply = e.Reply; Invoke((MethodInvoker)delegate { scanpbar.Value++; }); if ( scanpbar.Value == scanpbar.Maximum ) { Invoke((MethodInvoker)delegate { scantxtIPmin.Enabled = true; scantxtIPmax.Enabled = true; scanbtnNetwork.Enabled = true; scanbtnLaunch.Enabled = true; scancheckNetwork.Enabled = true; scanbtnExport.Enabled = true; }); } if ( e.Cancelled ) { MessageBox.Show("Ping avort"); } else if ( e.Error != null ) { MessageBox.Show("Erreur de ping"); } else if ( e.Reply == null ) { MessageBox.Show("Pas de reponse"); } else { if ( reply.Status == IPStatus.Success ) { try { IPHostEntry hoteDistant = Dns.GetHostEntry(e.Reply.Address); updateData(hoteDistant, e.Reply); } catch { updateData(e.Reply); } } } } public void updateData(IPHostEntry infoHote, PingReply reply) { Invoke((MethodInvoker)delegate { scancheckHosts.Rows.Add(); scancheckHosts.Rows[scancheckHosts.Rows.Count - 1].Cells[0].Value
= true;
scancheckHosts.Rows[scancheckHosts.Rows.Count - 1].Cells[1].Value = Image.FromFile(Environment.CurrentDirectory + @"\res\ordi.png"); try { scancheckHosts.Rows[scancheckHosts.Rows.Count 1].Cells[2].Value = infoHote.HostName; scancheckHosts.Rows[scancheckHosts.Rows.Count 1].Cells[3].Value = infoHote.AddressList[0].ToString(); Ordi host = new Ordi(); host.nomOrdi = scancheckHosts.Rows[scancheckHosts.Rows.Count - 1].Cells[2].Value.ToString(); host.ipOrdi = infoHote.AddressList[0].ToString(); hostList.Add(host); scancheckHosts.Update(); } catch { } }); } public void updateData(PingReply reply) { Invoke((MethodInvoker)delegate { scancheckHosts.Rows.Add(); scancheckHosts.Rows[scancheckHosts.Rows.Count - 1].Cells[0].Value = false;
scancheckHosts.Rows[scancheckHosts.Rows.Count - 1].Cells[1].Value = Image.FromFile(Environment.CurrentDirectory + @"\res\swarning.png"); scancheckHosts.Rows[scancheckHosts.Rows.Count - 1].Cells[2].Value = "Inconnu"; scancheckHosts.Rows[scancheckHosts.Rows.Count - 1].Cells[3].Value = reply.Address.ToString(); scancheckHosts.Update(); }); } /* SELECTION */ private void scancheckHosts_CellClick(object sender, DataGridViewCellEventArgs e) { if ( scancheckHosts.Rows[e.RowIndex].Cells[0].FormattedValue.Equals(false) ) { scancheckHosts.Rows[e.RowIndex].Cells[0].Value = true; } else { scancheckHosts.Rows[e.RowIndex].Cells[0].Value = false; } } /* EXPORT DE LA SELECTION */ private void scanbtnExport_Click(object sender, EventArgs e) { if ( hostList.Count == 0 ) { } else { DialogResult rep = MessageBox.Show("Voulez-vous exporter les lments slectionns?", "Export", MessageBoxButtons.YesNo); // int x = 0; if ( rep == DialogResult.Yes ) { StreamWriter sw = new StreamWriter(Environment.CurrentDirectory + @"\log\Scan-" + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + ".txt", true); sw.WriteLine(":BASE"); for ( int i = 0; i < scancheckHosts.Rows.Count; i++ ) { if ( scancheckHosts.Rows[i].Cells[0].FormattedValue.Equals(true) ) { foreach ( Ordi o in hostList ) { if ( o.nomOrdi.Contains(scancheckHosts.Rows[i].Cells[2].Value.ToString()) ) { sw.WriteLine(o.nomOrdi + "," + o.ipOrdi); } } } } sw.Close(); } } if ( Directory.Exists(Environment.CurrentDirectory + @"\log\temp") ) { foreach ( string file in Directory.GetFiles(Environment.CurrentDirectory + @"\log\temp") ) { File.Delete(file); } Directory.Delete(Environment.CurrentDirectory + @"\log\temp"); } } #endregion #region FICHIER /* MENU ET AFFICHAGE */ private void gestionfilecomboFiles_DropDownOpening(object sender, EventArgs e) { } updateList(gestionfilecomboFiles, "log");
private void updateList(ToolStripMenuItem btn, string path) { btn.DropDownItems.Clear(); string s; foreach (String file in Directory.GetFiles(path)) { s = file.Substring(file.LastIndexOf('\\') + 1); s = s.Remove(s.LastIndexOf('.')); btn.DropDownItems.Add(s); } } //private void updateListDir(ToolStripMenuItem btn, string path) //{ // btn.DropDownItems.Clear(); // string s; // foreach (String file in Directory.GetDirectories(path)) // { // s = file.Substring(file.LastIndexOf('\\') + 1); // if (s != "scr") // { // btn.DropDownItems.Add(s); // } // } //} private void gestionfilecomboFiles_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) { gestionfilecomboFiles.Tag = e.ClickedItem.Text; foreach ( String file in Directory.GetFiles("log") ) { if ( file.Contains(gestionfilecomboFiles.Tag.ToString()) ) { useFile = file; } } remplissageTreeview(); } private void remplissageTreeview() { gestiontreeControl.Nodes.Clear(); String[][] tabletree = allLinesInFile(useFile, ":"); foreach ( string s in tabletree[0] ) { string st0 = s.Replace(":", ""); TreeNode noeud = gestiontreeControl.Nodes.Add(st0); foreach ( TreeNode t in gestiontreeControl.Nodes ) { if ( t == noeud ) { foreach ( string sd in tabletree[( t.Index + 1 )] ) { string[] st1 = sd.Split(','); t.Nodes.Add(st1[0]); } } } } } private String[][] allLinesInFile(string file, string str) { int tt = 0; int indextt = 0; int indexlist = 0; string line = ""; List<int> counter = countLinesInFile(file, str); String[][] table = new String[( counter[0] + 1 )][]; table[tt] = new string[counter[0]]; StreamReader sr = new StreamReader(file); while ( ( line = sr.ReadLine() ) != null ) { if ( line.StartsWith(":") ) { tt = 0; indextt = 0;
foreach ( string s in table[0] ) { if ( s == null ) { table[tt][indextt] = line; indextt++; break; } else { indextt++; } } //tt=table[0].Length; indexlist++; table[indextt] = new string[counter[indexlist]]; tt = indextt; indextt = 0; } else { table[tt][indextt] = line; indextt++; } } sr.Close(); return table; } private List<int> countLinesInFile(string file, string str) { List<int> counter = new List<int>(); string line; StreamReader sr = new StreamReader(file); while ( ( line = sr.ReadLine() ) != null ) { if ( line.StartsWith(str) ) { if ( counter.Count == 0 ) { counter.Add(0); } counter[0]++; counter.Add(0); } else { if ( counter[( counter[0] )].ToString() == "" ) { counter.Add(0); } counter[( counter[0] )]++; } } sr.Close(); return counter; } /* FONCTIONS FICHIERS */ private void renameFile(string file) { if ( File.Exists(file) ) { String[] arg = { "Nouveau nom :" }; string nom = ""; newWTSin(arg); foreach ( string line in File.ReadLines(Environment.CurrentDirectory + @"\scr\arguments.txt") ) { nom = line; } nom = Environment.CurrentDirectory + @"\log\" + nom + ".txt"; Microsoft.VisualBasic.FileSystem.Rename(file, nom); useFile = nom; } } private void saveFile(string file) { if ( File.Exists(file) ) { } } private void deleteFile(string file) { if ( File.Exists(file) ) { DialogResult rep = MessageBox.Show("Supprimer le fichier slectionn?", "Suppression", MessageBoxButtons.YesNo); // int x = 0; if ( rep == DialogResult.Yes ) {
private void gestionfilebtnSave_Click(object sender, EventArgs e) { if ( gestionfilecomboFiles.Tag != null ) { saveFile(useFile); MessageBox.Show("SAUVEGARDE!!!!!"); updateList(gestionfilecomboFiles, "log"); } } private void gestionfilebtnRename_Click(object sender, EventArgs e) { if ( gestionfilecomboFiles.Tag != null ) { renameFile(useFile); updateList(gestionfilecomboFiles, "log"); } } private void gestionfilebtnDelete_Click(object sender, EventArgs e) { if ( gestionfilecomboFiles.Tag != null ) { deleteFile(useFile); updateList(gestionfilecomboFiles, "log"); } } #endregion #region SCRIPT private void newWTSin(String[] arguments) { if ( File.Exists(Environment.CurrentDirectory + @"\scr\arguments.txt") ) { File.Delete(Environment.CurrentDirectory + @"\scr\arguments.txt"); } List<String> reponse = new List<string>(); DialogResult dr = new DialogResult(); for ( int i = 0; i < arguments.Length; i++ ) { WTSin wtsin = new WTSin(arguments[i]); dr = wtsin.ShowDialog(this); } if ( dr == DialogResult.Yes ) { foreach ( string line in File.ReadLines(Environment.CurrentDirectory + @"\scr\arguments.txt") ) { reponse.Add(line); } } else if ( dr == DialogResult.No ) { if ( File.Exists(Environment.CurrentDirectory + @"\scr\arguments.txt") ) { File.Delete(Environment.CurrentDirectory + @"\scr\arguments.txt"); } } } { private void gestiontreeControl_AfterCheck(object sender, TreeViewEventArgs e) foreach ( TreeNode t in e.Node.Nodes ) { t.Checked = e.Node.Checked; } }
private void gestionscriptcomboScript_DropDownOpening(object sender, EventArgs e) { } private void gestionscriptcomboScript_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) { gestionscriptcomboScript.Tag = e.ClickedItem.Text; sortieTxt.Clear(); foreach ( String file in Directory.GetFiles("scr") ) { if ( file.Contains(gestionscriptcomboScript.Tag.ToString()) ) { scriptFile = file; break; } } bool continuer = msgWarning(); foreach ( String line in File.ReadLines(scriptFile) ) { if ( continuer == false ) { break; } else { if ( line.Contains("::ARG,") ) { newWTSin(recupereArguments(scriptFile)); } } } if (continuer == true) { constructionBat(scriptFile); lancementBat(scriptFile); } } private bool msgWarning() { bool continuer = true; foreach (String line in File.ReadLines(scriptFile)) { if (line.Contains("::INFO,")) { string s = line.Replace("::INFO,", ""); DialogResult dr = MessageBox.Show(s, "Warning", MessageBoxButtons.YesNo); if (dr == DialogResult.No) { continuer = false; } } } return continuer; } private String[] recupereArguments(string file) { string ligne = ""; foreach ( string line in File.ReadLines(file) ) { if ( line.Contains("::ARG,") ) { ligne = line; break; } } ligne = ligne.Replace("::ARG,", ""); String[] args = ligne.Split(','); return args; } private void constructionBat(string file) { List<String> linesTxt = new List<string>(); string ligne = ""; updateList(gestionscriptcomboScript, "scr");
if ( File.Exists(Environment.CurrentDirectory + @"\scr\scr\" + gestionscriptcomboScript.Tag.ToString() + ".bat") ) { File.Delete(Environment.CurrentDirectory + @"\scr\scr\" + gestionscriptcomboScript.Tag.ToString() + ".bat"); } foreach ( TreeNode t in gestiontreeControl.Nodes ) { foreach ( TreeNode tb in t.Nodes ) { if ( tb.Checked == true && tb.Level == 1 ) { foreach ( string line in File.ReadLines(file) ) { ligne = line; linesTxt.Add(ligne.Replace("%POSTE%", tb.Text)); } } } } StreamWriter sw = new StreamWriter(Environment.CurrentDirectory + @"\scr\scr\" + gestionscriptcomboScript.Tag.ToString() + ".bat", true); foreach ( string s in linesTxt ) { sw.WriteLine(s); } sw.Close(); } private void lancementBat(string file) { string arguments = ""; Process process = new Process(); process.StartInfo.FileName = Environment.CurrentDirectory + @"\scr\scr\" + gestionscriptcomboScript.Tag.ToString() + ".bat"; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; if ( File.Exists(Environment.CurrentDirectory + @"\scr\arguments.txt") ) { foreach ( string s in File.ReadLines(Environment.CurrentDirectory + @"\scr\arguments.txt") ) { arguments += s + " "; } process.StartInfo.Arguments = arguments; } process.OutputDataReceived += new DataReceivedEventHandler(OutPutEvent); process.Start(); process.BeginOutputReadLine(); while (!process.HasExited) { Application.DoEvents(); } if ( File.Exists(Environment.CurrentDirectory + @"\scr\arguments.txt") ) { File.Delete(Environment.CurrentDirectory + @"\scr\arguments.txt"); } journalisation(); } private void journalisation() { StreamWriter sw = new StreamWriter(Environment.CurrentDirectory + @"\journal\" + DateTime.Now.Day + "_" + DateTime.Now.Month + "_" + DateTime.Now.Year+".txt", true); string sDate = DateTime.Now.Hour + ":" + DateTime.Now.Minute; foreach (string line in sortieTxt.Lines) { sw.WriteLine(sDate.PadRight(12)+line); } sw.Close(); } private void OutPutEvent(object sendingProcess,DataReceivedEventArgs outLine) { if (outLine.Data != null) {
if (outLine.Data.Contains("COM")) { string ligne = outLine.Data; ligne = ligne.Replace("COM", "")+"\n"; Invoke((MethodInvoker)delegate { sortieTxt.AppendText(ligne); if (outLine.Data.Contains ("COME")){ sortieTxt.SelectionStart = sortieTxt.GetFirstCharIndexFromLine(sortieTxt.Lines.Length-1); sortieTxt.SelectionLength = sortieTxt.Lines[sortieTxt.Lines.Length-1].Length; sortieTxt.SelectionColor = System.Drawing.Color.Red; } }); } } } #endregion } }