簡単に説明するとPingで疎通確認して通信可能であればログインしてファイルをダウンロードするといったプログラムです。文字数の関係上、usingや宣言文は省略しています。
初心者の為どの様に書き換えたほうが効率がよくきれいなソースになるか等具体的に教えていただきたいです。
C#
1 public partial class Form1 : Form 2 { 3 public Form1() 4 { 5 InitializeComponent(); 6 } 7 8 //3秒ごとに動くタイマー 9 public void Timers(EventHandler eventHandler) 10 { 11 var timer = new System.Windows.Forms.Timer(); 12 timer.Tick += new EventHandler(eventHandler); 13 timer.Interval = 3000; 14 timer.Start(); 15 } 16 //numericUpDown1 * 60秒 でループするタイマー 17 public void loop(EventHandler eventHandler) 18 { 19 int intVal = Decimal.ToInt32(numericUpDown1.Value); 20 var timer = new System.Windows.Forms.Timer(); 21 timer.Tick += new EventHandler(eventHandler); 22 timer.Interval = intVal * 60000; 23 timer.Start(); 24 } 25 //メイン処理 26 public void button1_Click(object sender, EventArgs e) 27 { 28 first(); 29 loop(main); 30 } 31 public void icmp() 32 { 33 strt: 34 System.Threading.Thread.Sleep(1000); 35 label1.Text = "通信確認中"; 36 label1.Refresh(); 37 Ping ins_Ping = new Ping(); 38 string host = textBox0.Text; 39 int timeout = 1000; 40 try 41 { 42 PingReply ins_PingReply = ins_Ping.Send(host, timeout); 43 if (ins_PingReply.Status != IPStatus.Success) 44 { 45 label1.Text = "通信失敗"; 46 label1.Refresh(); 47 System.Threading.Thread.Sleep(5000); 48 goto strt; 49 } 50 else 51 { 52 label1.Text = "通信成功"; 53 label1.Refresh(); 54 System.Threading.Thread.Sleep(5000); 55 } 56 } 57 catch (Exception r) when (r is ArgumentNullException || r is InvalidOperationException || r is PingException || r is ObjectDisposedException) 58 { 59 label1.Text = "通信失敗"; 60 label1.Refresh(); 61 // MessageBox.Show(ex.Message); 62 System.Threading.Thread.Sleep(1000); 63 goto strt; 64 } 65 } 66 //初回動作用 67 public void first() 68 { 69 try 70 { 71 //System.Windows.Forms.WebBrowser IE11に変更 72 regKey.SetValue(strProcessName, 11001, Microsoft.Win32.RegistryValueKind.DWord); 73 icmp(); 74 //タイマー1を設定 75 Timers(MyClock1); 76 label1.Text = "接続中"; 77 label1.Refresh(); 78 //接続 79 //ブラウザのサイズを変更する 80 webBrowser1.Visible = true; 81 webBrowser1.Navigate("https://" + textBox0.Text); 82 //ページの読み込み完了まで待つ 83 while (webBrowser1.IsBusy || webBrowser1.ReadyState != WebBrowserReadyState.Complete) 84 { 85 System.Windows.Forms.Application.DoEvents(); 86 System.Threading.Thread.Sleep(100); 87 } 88 //1番目フレーム内のNameを取得 89 HtmlWindow iframe = webBrowser1.Document.Window.Frames[0]; 90 HtmlElementCollection elements = iframe.Document.All; 91 HtmlElement id = elements.GetElementsByName("userName")[0]; 92 HtmlElement pw = elements.GetElementsByName("pwd")[0]; 93 //ID・passを入力 94 id.InnerText = textBox1.Text; 95 pw.InnerText = textBox2.Text; 96 //Submitボタンをクリック 97 HtmlElement login = elements.GetElementsByName("Submit")[0]; 98 login.InvokeMember("click"); 99 //タイマー2を設定 100 Timers(MyClock2); 101 //ダウンロードページを開く 102 webBrowser2.Visible = true; 103 webBrowser2.Navigate("https://" + textBox0.Text + "/techSupport_.wri"); 104 label1.Text = "ダウンロード処理中"; 105 //タイマー3を設定 106 Timers(MyClock3); 107 Timers(MyClock4); 108 }catch (Exception e)when (e is ObjectDisposedException || e is InvalidOperationException) 109 { 110 System.Threading.Thread.Sleep(1000); 111 first(); 112 } 113 } 114 private void main(object sender, EventArgs e) 115 { 116 //mainのソースから不必要なソースを少し削ったソースが入っています 117 }catch (Exception w) when (w is ObjectDisposedException || w is InvalidOperationException || w is ArgumentOutOfRangeException) 118 { 119 System.Threading.Thread.Sleep(1000); 120 } 121} 122 //「セキュリティの警告」を承認する 123 private void MyClock1(object sender, EventArgs e) 124 { 125 IntPtr hWnd = FindWindow("#32770", "セキュリティの警告"); 126 if (hWnd != IntPtr.Zero)// 127 { 128 const uint WM_LBUTTONDOWN0 = 0x0201; 129 const uint WM_LBUTTONUP0 = 0x0202; 130 IntPtr hChild0; 131 hChild0 = IntPtr.Zero; 132 hChild0 = FindWindowEx(hWnd, IntPtr.Zero, "Button", "はい(&Y)"); 133 // はい(&Y)ボタンを押す 134 PostMessage(hChild0, WM_LBUTTONDOWN0, IntPtr.Zero, IntPtr.Zero); 135 PostMessage(hChild0, WM_LBUTTONUP0, IntPtr.Zero, IntPtr.Zero); 136 } 137 } 138 //「ファイルのダウンロード」の保存ボタンを押す 139 private void MyClock2(object sender, EventArgs e) 140 { 141 IntPtr hWnd2 = FindWindow("#32770", "ファイルのダウンロード"); 142 if (hWnd2 != IntPtr.Zero)// 143 { 144 const uint WM_LBUTTONDOWN1 = 0x0201; 145 const uint WM_LBUTTONUP1 = 0x0202; 146 IntPtr hChild1; 147 hChild1 = IntPtr.Zero; 148 hChild1 = FindWindowEx(hWnd2, IntPtr.Zero, "Button", "保存(&S)"); 149 // Saveボタンを押す 150 PostMessage(hChild1, WM_LBUTTONDOWN1, IntPtr.Zero, IntPtr.Zero); 151 PostMessage(hChild1, WM_LBUTTONUP1, IntPtr.Zero, IntPtr.Zero); 152 } 153 } 154 //「名前をつけて保存」の処理 155 private void MyClock3(object sender, EventArgs e) 156 { 157 IntPtr hWnd3 = FindWindow("#32770", "名前を付けて保存"); 158 if (hWnd3 != IntPtr.Zero)// 159 { 160 String now = DateTime.Now.ToString("yyyyMMddHHmmss"); 161 string filepath = (@label6.Text + "\" + now + ".txt"); // 保存先 162 const uint WM_SETTEXT = 0x000c; 163 const uint WM_LBUTTONDOWN = 0x0201; 164 const uint WM_LBUTTONUP = 0x0202; 165 IntPtr hChild; 166 IntPtr hEdit; 167 hChild = FindWindowEx(hWnd3, IntPtr.Zero, "DUIViewWndClassName", null); 168 hChild = FindWindowEx(hChild, IntPtr.Zero, "DirectUIHWND", null); 169 hChild = FindWindowEx(hChild, IntPtr.Zero, "FloatNotifySink", null); 170 hChild = FindWindowEx(hChild, IntPtr.Zero, "ComboBox", null); 171 hEdit = FindWindowEx(hChild, IntPtr.Zero, "Edit", null); 172 // ファイル名を設定する 173 SendMessage(hChild, WM_SETTEXT, IntPtr.Zero, filepath); 174 System.Threading.Thread.Sleep(2000); 175 // Saveボタンを見つける 176 hChild = IntPtr.Zero; 177 hChild = FindWindowEx(hWnd3, IntPtr.Zero, "Button", "保存(&S)"); 178 // Saveボタンを押す 179 PostMessage(hChild, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero); 180 PostMessage(hChild, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero); 181 } 182 } 183 //「ダウンロードの完了」を閉じる 184 private void MyClock4(object sender, EventArgs e) 185 { 186 IntPtr hWnd4 = FindWindow("#32770", "ダウンロードの完了"); 187 if (hWnd4 != IntPtr.Zero) 188 { 189 const uint WM_LBUTTONDOWN4 = 0x0201; 190 const uint WM_LBUTTONUP4 = 0x0202; 191 IntPtr hChild4; 192 hChild4 = IntPtr.Zero; 193 hChild4 = FindWindowEx(hWnd4, IntPtr.Zero, "Button", "閉じる"); 194 // Saveボタンを押す 195 PostMessage(hChild4, WM_LBUTTONDOWN4, IntPtr.Zero, IntPtr.Zero); 196 PostMessage(hChild4, WM_LBUTTONUP4, IntPtr.Zero, IntPtr.Zero); 197 DateTime at = DateTime.Now; 198 at = at.AddMinutes(Decimal.ToInt32(numericUpDown1.Value)); 199 label1.Text = "保存完了"; 200 label1.Refresh(); 201 } 202 203 } 204 private void button2_Click(object sender, EventArgs e) 205 { 206 //アプリケーションを終了させる 207 Application.Exit(); 208 } 209 private void button3_Click(object sender, EventArgs e) 210 { 211 // FolderBrowserDialogクラスのインスタンス生成 212 FolderBrowserDialog fbd = new FolderBrowserDialog(); 213 // ダイアログタイトルを設定 214 fbd.Description = "フォルダを選択してください"; 215 fbd.RootFolder = Environment.SpecialFolder.Desktop; 216 fbd.SelectedPath = @"c:\test\"; 217 fbd.ShowNewFolderButton = true; 218 if (fbd.ShowDialog() == Di 219alogResult.OK) 220 { 221 string myPath = fbd.SelectedPath; 222 label6.Text = myPath; 223 } 224 } 225 } 226}
回答1件
あなたの回答
tips
プレビュー