質問編集履歴

14

不正削除ロールバック

2018/09/20 00:35

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- ああああああああああああ
1
+ C# ソースコードの添削希望 回答希望
test CHANGED
@@ -1 +1,459 @@
1
+ 簡単に説明するとPingで疎通確認して通信可能であればログインしてファイルをダウンロードするといったプログラムです。文字数の関係上、usingや宣言文は省略しています。
2
+
3
+ 初心者の為どの様に書き換えたほうが効率がよくきれいなソースになるか等具体的に教えていただきたいです。
4
+
5
+ ```C#
6
+
7
+ public partial class Form1 : Form
8
+
9
+ {
10
+
11
+ public Form1()
12
+
13
+ {
14
+
15
+ InitializeComponent(); 
16
+
17
+ }
18
+
19
+
20
+
21
+ //3秒ごとに動くタイマー
22
+
23
+ public void Timers(EventHandler eventHandler)
24
+
25
+ {
26
+
27
+ var timer = new System.Windows.Forms.Timer();
28
+
29
+ timer.Tick += new EventHandler(eventHandler);
30
+
31
+ timer.Interval = 3000;
32
+
33
+ timer.Start();
34
+
35
+ }
36
+
37
+ //numericUpDown1 * 60秒 でループするタイマー
38
+
39
+ public void loop(EventHandler eventHandler)
40
+
41
+ {
42
+
43
+ int intVal = Decimal.ToInt32(numericUpDown1.Value);
44
+
45
+ var timer = new System.Windows.Forms.Timer();
46
+
47
+ timer.Tick += new EventHandler(eventHandler);
48
+
49
+ timer.Interval = intVal * 60000;
50
+
51
+ timer.Start();
52
+
53
+ }
54
+
55
+ //メイン処理
56
+
57
+ public void button1_Click(object sender, EventArgs e)
58
+
59
+ {
60
+
61
+ first();
62
+
63
+ loop(main);
64
+
65
+ }
66
+
67
+ public void icmp()
68
+
69
+ {
70
+
71
+ strt:
72
+
73
+ System.Threading.Thread.Sleep(1000);
74
+
75
+ label1.Text = "通信確認中";
76
+
77
+ label1.Refresh();
78
+
79
+ Ping ins_Ping = new Ping();
80
+
81
+ string host = textBox0.Text;
82
+
83
+ int timeout = 1000;
84
+
85
+ try
86
+
87
+ {
88
+
89
+ PingReply ins_PingReply = ins_Ping.Send(host, timeout);
90
+
91
+ if (ins_PingReply.Status != IPStatus.Success)
92
+
93
+ {
94
+
95
+ label1.Text = "通信失敗";
96
+
97
+ label1.Refresh();
98
+
99
+ System.Threading.Thread.Sleep(5000);
100
+
101
+ goto strt;
102
+
103
+ }
104
+
105
+ else
106
+
107
+ {
108
+
109
+ label1.Text = "通信成功";
110
+
111
+ label1.Refresh();
112
+
113
+ System.Threading.Thread.Sleep(5000);
114
+
115
+ }
116
+
117
+ }
118
+
119
+ catch (Exception r) when (r is ArgumentNullException || r is InvalidOperationException || r is PingException || r is ObjectDisposedException)
120
+
121
+ {
122
+
123
+ label1.Text = "通信失敗";
124
+
125
+ label1.Refresh();
126
+
1
- あああああああああああああああああああああああああああああああああああああああああ
127
+ // MessageBox.Show(ex.Message);
128
+
129
+ System.Threading.Thread.Sleep(1000);
130
+
131
+ goto strt;
132
+
133
+ }
134
+
135
+ }
136
+
137
+ //初回動作用
138
+
139
+ public void first()
140
+
141
+ {
142
+
143
+ try
144
+
145
+ {
146
+
147
+ //System.Windows.Forms.WebBrowser IE11に変更
148
+
149
+ regKey.SetValue(strProcessName, 11001, Microsoft.Win32.RegistryValueKind.DWord);
150
+
151
+ icmp();
152
+
153
+ //タイマー1を設定
154
+
155
+ Timers(MyClock1);
156
+
157
+ label1.Text = "接続中";
158
+
159
+ label1.Refresh();
160
+
161
+ //接続
162
+
163
+ //ブラウザのサイズを変更する
164
+
165
+ webBrowser1.Visible = true;
166
+
167
+ webBrowser1.Navigate("https://" + textBox0.Text);
168
+
169
+ //ページの読み込み完了まで待つ
170
+
171
+ while (webBrowser1.IsBusy || webBrowser1.ReadyState != WebBrowserReadyState.Complete)
172
+
173
+ {
174
+
175
+ System.Windows.Forms.Application.DoEvents();
176
+
177
+ System.Threading.Thread.Sleep(100);
178
+
179
+ }
180
+
181
+ //1番目フレーム内のNameを取得
182
+
183
+ HtmlWindow iframe = webBrowser1.Document.Window.Frames[0];
184
+
185
+ HtmlElementCollection elements = iframe.Document.All;
186
+
187
+ HtmlElement id = elements.GetElementsByName("userName")[0];
188
+
189
+ HtmlElement pw = elements.GetElementsByName("pwd")[0];
190
+
191
+ //ID・passを入力
192
+
193
+ id.InnerText = textBox1.Text;
194
+
195
+ pw.InnerText = textBox2.Text;
196
+
197
+ //Submitボタンをクリック
198
+
199
+ HtmlElement login = elements.GetElementsByName("Submit")[0];
200
+
201
+ login.InvokeMember("click");
202
+
203
+ //タイマー2を設定
204
+
205
+ Timers(MyClock2);
206
+
207
+ //ダウンロードページを開く
208
+
209
+ webBrowser2.Visible = true;
210
+
211
+ webBrowser2.Navigate("https://" + textBox0.Text + "/techSupport_.wri");
212
+
213
+ label1.Text = "ダウンロード処理中";
214
+
215
+ //タイマー3を設定
216
+
217
+ Timers(MyClock3);
218
+
219
+ Timers(MyClock4);
220
+
221
+ }catch (Exception e)when (e is ObjectDisposedException || e is InvalidOperationException)
222
+
223
+ {
224
+
225
+ System.Threading.Thread.Sleep(1000);
226
+
227
+ first();
228
+
229
+ }
230
+
231
+ }
232
+
233
+ private void main(object sender, EventArgs e)
234
+
235
+ {
236
+
237
+ //mainのソースから不必要なソースを少し削ったソースが入っています
238
+
239
+ }catch (Exception w) when (w is ObjectDisposedException || w is InvalidOperationException || w is ArgumentOutOfRangeException)
240
+
241
+ {
242
+
243
+ System.Threading.Thread.Sleep(1000);
244
+
245
+ }
246
+
247
+ }
248
+
249
+   //「セキュリティの警告」を承認する
250
+
251
+ private void MyClock1(object sender, EventArgs e)
252
+
253
+ {
254
+
255
+ IntPtr hWnd = FindWindow("#32770", "セキュリティの警告");
256
+
257
+ if (hWnd != IntPtr.Zero)//
258
+
259
+ {
260
+
261
+ const uint WM_LBUTTONDOWN0 = 0x0201;
262
+
263
+ const uint WM_LBUTTONUP0 = 0x0202;
264
+
265
+ IntPtr hChild0;
266
+
267
+ hChild0 = IntPtr.Zero;
268
+
269
+ hChild0 = FindWindowEx(hWnd, IntPtr.Zero, "Button", "はい(&Y)");
270
+
271
+ // はい(&Y)ボタンを押す
272
+
273
+ PostMessage(hChild0, WM_LBUTTONDOWN0, IntPtr.Zero, IntPtr.Zero);
274
+
275
+ PostMessage(hChild0, WM_LBUTTONUP0, IntPtr.Zero, IntPtr.Zero);
276
+
277
+ }
278
+
279
+ }
280
+
281
+ //「ファイルのダウンロード」の保存ボタンを押す
282
+
283
+ private void MyClock2(object sender, EventArgs e)
284
+
285
+ {
286
+
287
+ IntPtr hWnd2 = FindWindow("#32770", "ファイルのダウンロード");
288
+
289
+ if (hWnd2 != IntPtr.Zero)//
290
+
291
+ {
292
+
293
+ const uint WM_LBUTTONDOWN1 = 0x0201;
294
+
295
+ const uint WM_LBUTTONUP1 = 0x0202;
296
+
297
+ IntPtr hChild1;
298
+
299
+ hChild1 = IntPtr.Zero;
300
+
301
+ hChild1 = FindWindowEx(hWnd2, IntPtr.Zero, "Button", "保存(&S)");
302
+
303
+ // Saveボタンを押す
304
+
305
+ PostMessage(hChild1, WM_LBUTTONDOWN1, IntPtr.Zero, IntPtr.Zero);
306
+
307
+ PostMessage(hChild1, WM_LBUTTONUP1, IntPtr.Zero, IntPtr.Zero);
308
+
309
+ }
310
+
311
+ }
312
+
313
+ //「名前をつけて保存」の処理
314
+
315
+ private void MyClock3(object sender, EventArgs e)
316
+
317
+ {
318
+
319
+ IntPtr hWnd3 = FindWindow("#32770", "名前を付けて保存");
320
+
321
+ if (hWnd3 != IntPtr.Zero)//
322
+
323
+ {
324
+
325
+ String now = DateTime.Now.ToString("yyyyMMddHHmmss");
326
+
327
+ string filepath = (@label6.Text + "\" + now + ".txt"); // 保存先
328
+
329
+ const uint WM_SETTEXT = 0x000c;
330
+
331
+ const uint WM_LBUTTONDOWN = 0x0201;
332
+
333
+ const uint WM_LBUTTONUP = 0x0202;
334
+
335
+ IntPtr hChild;
336
+
337
+ IntPtr hEdit;
338
+
339
+ hChild = FindWindowEx(hWnd3, IntPtr.Zero, "DUIViewWndClassName", null);
340
+
341
+ hChild = FindWindowEx(hChild, IntPtr.Zero, "DirectUIHWND", null);
342
+
343
+ hChild = FindWindowEx(hChild, IntPtr.Zero, "FloatNotifySink", null);
344
+
345
+ hChild = FindWindowEx(hChild, IntPtr.Zero, "ComboBox", null);
346
+
347
+ hEdit = FindWindowEx(hChild, IntPtr.Zero, "Edit", null);
348
+
349
+ // ファイル名を設定する
350
+
351
+ SendMessage(hChild, WM_SETTEXT, IntPtr.Zero, filepath);
352
+
353
+ System.Threading.Thread.Sleep(2000);
354
+
355
+ // Saveボタンを見つける
356
+
357
+ hChild = IntPtr.Zero;
358
+
359
+ hChild = FindWindowEx(hWnd3, IntPtr.Zero, "Button", "保存(&S)");
360
+
361
+ // Saveボタンを押す
362
+
363
+ PostMessage(hChild, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
364
+
365
+ PostMessage(hChild, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
366
+
367
+ }
368
+
369
+ }
370
+
371
+ //「ダウンロードの完了」を閉じる
372
+
373
+ private void MyClock4(object sender, EventArgs e)
374
+
375
+ {
376
+
377
+ IntPtr hWnd4 = FindWindow("#32770", "ダウンロードの完了");
378
+
379
+ if (hWnd4 != IntPtr.Zero)
380
+
381
+ {
382
+
383
+ const uint WM_LBUTTONDOWN4 = 0x0201;
384
+
385
+  const uint WM_LBUTTONUP4 = 0x0202;
386
+
387
+ IntPtr hChild4;
388
+
389
+ hChild4 = IntPtr.Zero;
390
+
391
+ hChild4 = FindWindowEx(hWnd4, IntPtr.Zero, "Button", "閉じる");
392
+
393
+ // Saveボタンを押す
394
+
395
+ PostMessage(hChild4, WM_LBUTTONDOWN4, IntPtr.Zero, IntPtr.Zero);
396
+
397
+ PostMessage(hChild4, WM_LBUTTONUP4, IntPtr.Zero, IntPtr.Zero);
398
+
399
+ DateTime at = DateTime.Now;
400
+
401
+ at = at.AddMinutes(Decimal.ToInt32(numericUpDown1.Value));
402
+
403
+ label1.Text = "保存完了";
404
+
405
+ label1.Refresh();
406
+
407
+ }
408
+
409
+
410
+
411
+ }
412
+
413
+ private void button2_Click(object sender, EventArgs e)
414
+
415
+ {
416
+
417
+ //アプリケーションを終了させる
418
+
419
+ Application.Exit();
420
+
421
+ }
422
+
423
+ private void button3_Click(object sender, EventArgs e)
424
+
425
+ {
426
+
427
+ // FolderBrowserDialogクラスのインスタンス生成
428
+
429
+ FolderBrowserDialog fbd = new FolderBrowserDialog();
430
+
431
+ // ダイアログタイトルを設定
432
+
433
+ fbd.Description = "フォルダを選択してください";
434
+
435
+ fbd.RootFolder = Environment.SpecialFolder.Desktop;
436
+
437
+ fbd.SelectedPath = @"c:\test\";
438
+
439
+ fbd.ShowNewFolderButton = true;
440
+
441
+ if (fbd.ShowDialog() == Di
442
+
443
+ alogResult.OK)
444
+
445
+ {
446
+
447
+ string myPath = fbd.SelectedPath;
448
+
449
+ label6.Text = myPath;
450
+
451
+ }
452
+
453
+ }
454
+
455
+ }
456
+
457
+ }
458
+
459
+ ```

13

2018/09/20 00:35

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- C# ソースコードの添削希望 回答希望
1
+ ああああああああああああ
test CHANGED
@@ -1,3 +1 @@
1
- 簡単に説明するとPingで疎通確認して通信可能であればログインしてファイルをダウンロードするといったプログラムです。文字数の関係上、usingや宣言文は省略しています。
2
-
3
- 初心者の為どの様に書き換えたほうが効率がよくきれいなソースになるか等具体的に教えていただきたいです。
1
+ あああああああああああああああああああああああああああああああああああああああああ

12

修正

2018/09/19 03:16

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,521 +1,3 @@
1
1
  簡単に説明するとPingで疎通確認して通信可能であればログインしてファイルをダウンロードするといったプログラムです。文字数の関係上、usingや宣言文は省略しています。
2
2
 
3
3
  初心者の為どの様に書き換えたほうが効率がよくきれいなソースになるか等具体的に教えていただきたいです。
4
-
5
- ```C#
6
-
7
- public partial class Form1 : Form
8
-
9
- {
10
-
11
- public Form1()
12
-
13
- {
14
-
15
- InitializeComponent(); 
16
-
17
- }
18
-
19
-
20
-
21
- //3秒ごとに動くタイマー
22
-
23
- public void Timers(EventHandler eventHandler)
24
-
25
- {
26
-
27
- var timer = new System.Windows.Forms.Timer();
28
-
29
- timer.Tick += new EventHandler(eventHandler);
30
-
31
- timer.Interval = 3000;
32
-
33
- timer.Start();
34
-
35
- }
36
-
37
-
38
-
39
- //numericUpDown1 * 60秒 でループするタイマー
40
-
41
- public void loop(EventHandler eventHandler)
42
-
43
- {
44
-
45
- int intVal = Decimal.ToInt32(numericUpDown1.Value);
46
-
47
- var timer = new System.Windows.Forms.Timer();
48
-
49
- timer.Tick += new EventHandler(eventHandler);
50
-
51
- timer.Interval = intVal * 60000;
52
-
53
- timer.Start();
54
-
55
- }
56
-
57
- //メイン処理
58
-
59
- public void button1_Click(object sender, EventArgs e)
60
-
61
- {
62
-
63
- first();
64
-
65
- loop(main);
66
-
67
-
68
-
69
- }
70
-
71
- public void icmp()
72
-
73
- {
74
-
75
- strt:
76
-
77
- System.Threading.Thread.Sleep(1000);
78
-
79
- label1.Text = "通信確認中";
80
-
81
- label1.Refresh();
82
-
83
- Ping ins_Ping = new Ping();
84
-
85
- string host = textBox0.Text;
86
-
87
- int timeout = 1000;
88
-
89
- try
90
-
91
- {
92
-
93
- PingReply ins_PingReply = ins_Ping.Send(host, timeout);
94
-
95
- if (ins_PingReply.Status != IPStatus.Success)
96
-
97
- {
98
-
99
- label1.Text = "通信失敗";
100
-
101
- label1.Refresh();
102
-
103
- System.Threading.Thread.Sleep(5000);
104
-
105
- goto strt;
106
-
107
- }
108
-
109
- else
110
-
111
- {
112
-
113
- label1.Text = "通信成功";
114
-
115
- label1.Refresh();
116
-
117
- System.Threading.Thread.Sleep(5000);
118
-
119
- }
120
-
121
- }
122
-
123
- catch (Exception r) when (r is ArgumentNullException || r is InvalidOperationException || r is PingException || r is ObjectDisposedException)
124
-
125
- {
126
-
127
- label1.Text = "通信失敗";
128
-
129
- label1.Refresh();
130
-
131
-
132
-
133
- // MessageBox.Show(ex.Message);
134
-
135
- System.Threading.Thread.Sleep(1000);
136
-
137
- goto strt;
138
-
139
-
140
-
141
- }
142
-
143
- }
144
-
145
- //初回動作用
146
-
147
- public void first()
148
-
149
- {
150
-
151
- try
152
-
153
- {
154
-
155
- //System.Windows.Forms.WebBrowser IE11に変更
156
-
157
- regKey.SetValue(strProcessName, 11001, Microsoft.Win32.RegistryValueKind.DWord);
158
-
159
- icmp();
160
-
161
-
162
-
163
- //タイマー1を設定
164
-
165
- Timers(MyClock1);
166
-
167
- label1.Text = "接続中";
168
-
169
- label1.Refresh();
170
-
171
- //接続
172
-
173
- //ブラウザのサイズを変更する
174
-
175
- webBrowser1.Visible = true;
176
-
177
- webBrowser1.Navigate("https://" + textBox0.Text);
178
-
179
-
180
-
181
- //ページの読み込み完了まで待つ
182
-
183
- while (webBrowser1.IsBusy || webBrowser1.ReadyState != WebBrowserReadyState.Complete)
184
-
185
- {
186
-
187
- System.Windows.Forms.Application.DoEvents();
188
-
189
- System.Threading.Thread.Sleep(100);
190
-
191
- }
192
-
193
- //1番目フレーム内のNameを取得
194
-
195
- HtmlWindow iframe = webBrowser1.Document.Window.Frames[0];
196
-
197
- HtmlElementCollection elements = iframe.Document.All;
198
-
199
- HtmlElement id = elements.GetElementsByName("userName")[0];
200
-
201
- HtmlElement pw = elements.GetElementsByName("pwd")[0];
202
-
203
- //ID・passを入力
204
-
205
- id.InnerText = textBox1.Text;
206
-
207
- pw.InnerText = textBox2.Text;
208
-
209
-
210
-
211
- //Submitボタンをクリック
212
-
213
- HtmlElement login = elements.GetElementsByName("Submit")[0];
214
-
215
- login.InvokeMember("click");
216
-
217
-
218
-
219
- //タイマー2を設定
220
-
221
- Timers(MyClock2);
222
-
223
-
224
-
225
- //ダウンロードページを開く
226
-
227
- webBrowser2.Visible = true;
228
-
229
- webBrowser2.Navigate("https://" + textBox0.Text + "/techSupport_.wri");
230
-
231
- label1.Text = "ダウンロード処理中";
232
-
233
-
234
-
235
- //タイマー3を設定
236
-
237
- Timers(MyClock3);
238
-
239
-
240
-
241
- Timers(MyClock4);
242
-
243
- }catch (Exception e)when (e is ObjectDisposedException || e is InvalidOperationException)
244
-
245
- {
246
-
247
- System.Threading.Thread.Sleep(1000);
248
-
249
- first();
250
-
251
-
252
-
253
- }
254
-
255
- }
256
-
257
- private void main(object sender, EventArgs e)
258
-
259
- {
260
-
261
- //mainのソースから不必要なソースを少し削ったソースが入っています
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
- }catch (Exception w) when (w is ObjectDisposedException || w is InvalidOperationException || w is ArgumentOutOfRangeException)
272
-
273
- {
274
-
275
- System.Threading.Thread.Sleep(1000);
276
-
277
-
278
-
279
- }
280
-
281
- }
282
-
283
-   //「セキュリティの警告」を承認する
284
-
285
- private void MyClock1(object sender, EventArgs e)
286
-
287
- {
288
-
289
- IntPtr hWnd = FindWindow("#32770", "セキュリティの警告");
290
-
291
-
292
-
293
- if (hWnd != IntPtr.Zero)//
294
-
295
- {
296
-
297
-
298
-
299
- const uint WM_LBUTTONDOWN0 = 0x0201;
300
-
301
- const uint WM_LBUTTONUP0 = 0x0202;
302
-
303
- IntPtr hChild0;
304
-
305
- hChild0 = IntPtr.Zero;
306
-
307
- hChild0 = FindWindowEx(hWnd, IntPtr.Zero, "Button", "はい(&Y)");
308
-
309
- // はい(&Y)ボタンを押す
310
-
311
- PostMessage(hChild0, WM_LBUTTONDOWN0, IntPtr.Zero, IntPtr.Zero);
312
-
313
- PostMessage(hChild0, WM_LBUTTONUP0, IntPtr.Zero, IntPtr.Zero);
314
-
315
- }
316
-
317
-
318
-
319
- }
320
-
321
- //「ファイルのダウンロード」の保存ボタンを押す
322
-
323
- private void MyClock2(object sender, EventArgs e)
324
-
325
- {
326
-
327
- IntPtr hWnd2 = FindWindow("#32770", "ファイルのダウンロード");
328
-
329
- if (hWnd2 != IntPtr.Zero)//
330
-
331
- {
332
-
333
- const uint WM_LBUTTONDOWN1 = 0x0201;
334
-
335
- const uint WM_LBUTTONUP1 = 0x0202;
336
-
337
- IntPtr hChild1;
338
-
339
- hChild1 = IntPtr.Zero;
340
-
341
- hChild1 = FindWindowEx(hWnd2, IntPtr.Zero, "Button", "保存(&S)");
342
-
343
- // Saveボタンを押す
344
-
345
- PostMessage(hChild1, WM_LBUTTONDOWN1, IntPtr.Zero, IntPtr.Zero);
346
-
347
- PostMessage(hChild1, WM_LBUTTONUP1, IntPtr.Zero, IntPtr.Zero);
348
-
349
- }
350
-
351
- }
352
-
353
- //「名前をつけて保存」の処理
354
-
355
- private void MyClock3(object sender, EventArgs e)
356
-
357
- {
358
-
359
- IntPtr hWnd3 = FindWindow("#32770", "名前を付けて保存");
360
-
361
- if (hWnd3 != IntPtr.Zero)//
362
-
363
- {
364
-
365
-
366
-
367
- String now = DateTime.Now.ToString("yyyyMMddHHmmss");
368
-
369
- string filepath = (@label6.Text + "\" + now + ".txt"); // 保存先
370
-
371
- const uint WM_SETTEXT = 0x000c;
372
-
373
- const uint WM_LBUTTONDOWN = 0x0201;
374
-
375
- const uint WM_LBUTTONUP = 0x0202;
376
-
377
- IntPtr hChild;
378
-
379
- IntPtr hEdit;
380
-
381
-
382
-
383
- hChild = FindWindowEx(hWnd3, IntPtr.Zero, "DUIViewWndClassName", null);
384
-
385
- hChild = FindWindowEx(hChild, IntPtr.Zero, "DirectUIHWND", null);
386
-
387
- hChild = FindWindowEx(hChild, IntPtr.Zero, "FloatNotifySink", null);
388
-
389
- hChild = FindWindowEx(hChild, IntPtr.Zero, "ComboBox", null);
390
-
391
-
392
-
393
- hEdit = FindWindowEx(hChild, IntPtr.Zero, "Edit", null);
394
-
395
- // ファイル名を設定する
396
-
397
- SendMessage(hChild, WM_SETTEXT, IntPtr.Zero, filepath);
398
-
399
- System.Threading.Thread.Sleep(2000);
400
-
401
- // Saveボタンを見つける
402
-
403
- hChild = IntPtr.Zero;
404
-
405
- hChild = FindWindowEx(hWnd3, IntPtr.Zero, "Button", "保存(&S)");
406
-
407
- // Saveボタンを押す
408
-
409
- PostMessage(hChild, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
410
-
411
- PostMessage(hChild, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
412
-
413
- }
414
-
415
- }
416
-
417
- //「ダウンロードの完了」を閉じる
418
-
419
- private void MyClock4(object sender, EventArgs e)
420
-
421
- {
422
-
423
- IntPtr hWnd4 = FindWindow("#32770", "ダウンロードの完了");
424
-
425
- if (hWnd4 != IntPtr.Zero)
426
-
427
- {
428
-
429
- const uint WM_LBUTTONDOWN4 = 0x0201;
430
-
431
-  const uint WM_LBUTTONUP4 = 0x0202;
432
-
433
- IntPtr hChild4;
434
-
435
- hChild4 = IntPtr.Zero;
436
-
437
- hChild4 = FindWindowEx(hWnd4, IntPtr.Zero, "Button", "閉じる");
438
-
439
- // Saveボタンを押す
440
-
441
- PostMessage(hChild4, WM_LBUTTONDOWN4, IntPtr.Zero, IntPtr.Zero);
442
-
443
- PostMessage(hChild4, WM_LBUTTONUP4, IntPtr.Zero, IntPtr.Zero);
444
-
445
-
446
-
447
- DateTime at = DateTime.Now;
448
-
449
- at = at.AddMinutes(Decimal.ToInt32(numericUpDown1.Value));
450
-
451
- label1.Text = "保存完了";
452
-
453
- label1.Refresh();
454
-
455
-
456
-
457
- }
458
-
459
-
460
-
461
-
462
-
463
- }
464
-
465
-
466
-
467
- private void button2_Click(object sender, EventArgs e)
468
-
469
- {
470
-
471
- //アプリケーションを終了させる
472
-
473
- Application.Exit();
474
-
475
- }
476
-
477
-
478
-
479
- private void button3_Click(object sender, EventArgs e)
480
-
481
- {
482
-
483
- // FolderBrowserDialogクラスのインスタンス生成
484
-
485
- FolderBrowserDialog fbd = new FolderBrowserDialog();
486
-
487
-
488
-
489
- // ダイアログタイトルを設定
490
-
491
- fbd.Description = "フォルダを選択してください";
492
-
493
- fbd.RootFolder = Environment.SpecialFolder.Desktop;
494
-
495
- fbd.SelectedPath = @"c:\test\";
496
-
497
- fbd.ShowNewFolderButton = true;
498
-
499
-
500
-
501
- if (fbd.ShowDialog() == Di
502
-
503
- alogResult.OK)
504
-
505
- {
506
-
507
- string myPath = fbd.SelectedPath;
508
-
509
- label6.Text = myPath;
510
-
511
-
512
-
513
- }
514
-
515
- }
516
-
517
- }
518
-
519
- }
520
-
521
- ```

11

題名変更

2018/09/18 06:46

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- C# ソースコードの添削希望
1
+ C# ソースコードの添削希望 回答希望
test CHANGED
File without changes

10

変更

2018/09/14 07:45

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  {
14
14
 
15
- InitializeComponent();
15
+ InitializeComponent(); 
16
16
 
17
17
  }
18
18
 
@@ -164,7 +164,7 @@
164
164
 
165
165
  Timers(MyClock1);
166
166
 
167
- label1.Text = "SonicWALL接続中";
167
+ label1.Text = "接続中";
168
168
 
169
169
  label1.Refresh();
170
170
 
@@ -222,7 +222,7 @@
222
222
 
223
223
 
224
224
 
225
- //診断レポートのダウンロードページを開く
225
+ //ダウンロードページを開く
226
226
 
227
227
  webBrowser2.Visible = true;
228
228
 

9

ソース

2018/09/14 04:52

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,7 @@
1
+ 簡単に説明するとPingで疎通確認して通信可能であればログインしてファイルをダウンロードするといったプログラムです。文字数の関係上、usingや宣言文は省略しています。
2
+
3
+ 初心者の為どの様に書き換えたほうが効率がよくきれいなソースになるか等具体的に教えていただきたいです。
4
+
1
5
  ```C#
2
6
 
3
7
  public partial class Form1 : Form

8

ソース

2018/09/14 04:50

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1 +1,517 @@
1
+ ```C#
2
+
3
+ public partial class Form1 : Form
4
+
5
+ {
6
+
7
+ public Form1()
8
+
9
+ {
10
+
11
+ InitializeComponent();
12
+
13
+ }
14
+
15
+
16
+
17
+ //3秒ごとに動くタイマー
18
+
19
+ public void Timers(EventHandler eventHandler)
20
+
21
+ {
22
+
23
+ var timer = new System.Windows.Forms.Timer();
24
+
25
+ timer.Tick += new EventHandler(eventHandler);
26
+
27
+ timer.Interval = 3000;
28
+
29
+ timer.Start();
30
+
31
+ }
32
+
33
+
34
+
35
+ //numericUpDown1 * 60秒 でループするタイマー
36
+
37
+ public void loop(EventHandler eventHandler)
38
+
39
+ {
40
+
41
+ int intVal = Decimal.ToInt32(numericUpDown1.Value);
42
+
43
+ var timer = new System.Windows.Forms.Timer();
44
+
45
+ timer.Tick += new EventHandler(eventHandler);
46
+
47
+ timer.Interval = intVal * 60000;
48
+
49
+ timer.Start();
50
+
51
+ }
52
+
53
+ //メイン処理
54
+
55
+ public void button1_Click(object sender, EventArgs e)
56
+
57
+ {
58
+
59
+ first();
60
+
61
+ loop(main);
62
+
63
+
64
+
65
+ }
66
+
67
+ public void icmp()
68
+
69
+ {
70
+
71
+ strt:
72
+
73
+ System.Threading.Thread.Sleep(1000);
74
+
75
+ label1.Text = "通信確認中";
76
+
77
+ label1.Refresh();
78
+
79
+ Ping ins_Ping = new Ping();
80
+
81
+ string host = textBox0.Text;
82
+
83
+ int timeout = 1000;
84
+
85
+ try
86
+
87
+ {
88
+
89
+ PingReply ins_PingReply = ins_Ping.Send(host, timeout);
90
+
91
+ if (ins_PingReply.Status != IPStatus.Success)
92
+
93
+ {
94
+
95
+ label1.Text = "通信失敗";
96
+
97
+ label1.Refresh();
98
+
99
+ System.Threading.Thread.Sleep(5000);
100
+
101
+ goto strt;
102
+
103
+ }
104
+
105
+ else
106
+
107
+ {
108
+
109
+ label1.Text = "通信成功";
110
+
111
+ label1.Refresh();
112
+
113
+ System.Threading.Thread.Sleep(5000);
114
+
115
+ }
116
+
117
+ }
118
+
119
+ catch (Exception r) when (r is ArgumentNullException || r is InvalidOperationException || r is PingException || r is ObjectDisposedException)
120
+
121
+ {
122
+
123
+ label1.Text = "通信失敗";
124
+
125
+ label1.Refresh();
126
+
127
+
128
+
129
+ // MessageBox.Show(ex.Message);
130
+
131
+ System.Threading.Thread.Sleep(1000);
132
+
133
+ goto strt;
134
+
135
+
136
+
137
+ }
138
+
139
+ }
140
+
141
+ //初回動作用
142
+
143
+ public void first()
144
+
145
+ {
146
+
147
+ try
148
+
149
+ {
150
+
151
+ //System.Windows.Forms.WebBrowser IE11に変更
152
+
153
+ regKey.SetValue(strProcessName, 11001, Microsoft.Win32.RegistryValueKind.DWord);
154
+
1
- 質問編集中です
155
+ icmp();
156
+
157
+
158
+
159
+ //タイマー1を設定
160
+
161
+ Timers(MyClock1);
162
+
163
+ label1.Text = "SonicWALL接続中";
164
+
165
+ label1.Refresh();
166
+
167
+ //接続
168
+
169
+ //ブラウザのサイズを変更する
170
+
171
+ webBrowser1.Visible = true;
172
+
173
+ webBrowser1.Navigate("https://" + textBox0.Text);
174
+
175
+
176
+
177
+ //ページの読み込み完了まで待つ
178
+
179
+ while (webBrowser1.IsBusy || webBrowser1.ReadyState != WebBrowserReadyState.Complete)
180
+
181
+ {
182
+
183
+ System.Windows.Forms.Application.DoEvents();
184
+
185
+ System.Threading.Thread.Sleep(100);
186
+
187
+ }
188
+
189
+ //1番目フレーム内のNameを取得
190
+
191
+ HtmlWindow iframe = webBrowser1.Document.Window.Frames[0];
192
+
193
+ HtmlElementCollection elements = iframe.Document.All;
194
+
195
+ HtmlElement id = elements.GetElementsByName("userName")[0];
196
+
197
+ HtmlElement pw = elements.GetElementsByName("pwd")[0];
198
+
199
+ //ID・passを入力
200
+
201
+ id.InnerText = textBox1.Text;
202
+
203
+ pw.InnerText = textBox2.Text;
204
+
205
+
206
+
207
+ //Submitボタンをクリック
208
+
209
+ HtmlElement login = elements.GetElementsByName("Submit")[0];
210
+
211
+ login.InvokeMember("click");
212
+
213
+
214
+
215
+ //タイマー2を設定
216
+
217
+ Timers(MyClock2);
218
+
219
+
220
+
221
+ //診断レポートのダウンロードページを開く
222
+
223
+ webBrowser2.Visible = true;
224
+
225
+ webBrowser2.Navigate("https://" + textBox0.Text + "/techSupport_.wri");
226
+
227
+ label1.Text = "ダウンロード処理中";
228
+
229
+
230
+
231
+ //タイマー3を設定
232
+
233
+ Timers(MyClock3);
234
+
235
+
236
+
237
+ Timers(MyClock4);
238
+
239
+ }catch (Exception e)when (e is ObjectDisposedException || e is InvalidOperationException)
240
+
241
+ {
242
+
243
+ System.Threading.Thread.Sleep(1000);
244
+
245
+ first();
246
+
247
+
248
+
249
+ }
250
+
251
+ }
252
+
253
+ private void main(object sender, EventArgs e)
254
+
255
+ {
256
+
257
+ //mainのソースから不必要なソースを少し削ったソースが入っています
258
+
259
+
260
+
261
+
262
+
263
+
264
+
265
+
266
+
267
+ }catch (Exception w) when (w is ObjectDisposedException || w is InvalidOperationException || w is ArgumentOutOfRangeException)
268
+
269
+ {
270
+
271
+ System.Threading.Thread.Sleep(1000);
272
+
273
+
274
+
275
+ }
276
+
277
+ }
278
+
279
+   //「セキュリティの警告」を承認する
280
+
281
+ private void MyClock1(object sender, EventArgs e)
282
+
283
+ {
284
+
285
+ IntPtr hWnd = FindWindow("#32770", "セキュリティの警告");
286
+
287
+
288
+
289
+ if (hWnd != IntPtr.Zero)//
290
+
291
+ {
292
+
293
+
294
+
295
+ const uint WM_LBUTTONDOWN0 = 0x0201;
296
+
297
+ const uint WM_LBUTTONUP0 = 0x0202;
298
+
299
+ IntPtr hChild0;
300
+
301
+ hChild0 = IntPtr.Zero;
302
+
303
+ hChild0 = FindWindowEx(hWnd, IntPtr.Zero, "Button", "はい(&Y)");
304
+
305
+ // はい(&Y)ボタンを押す
306
+
307
+ PostMessage(hChild0, WM_LBUTTONDOWN0, IntPtr.Zero, IntPtr.Zero);
308
+
309
+ PostMessage(hChild0, WM_LBUTTONUP0, IntPtr.Zero, IntPtr.Zero);
310
+
311
+ }
312
+
313
+
314
+
315
+ }
316
+
317
+ //「ファイルのダウンロード」の保存ボタンを押す
318
+
319
+ private void MyClock2(object sender, EventArgs e)
320
+
321
+ {
322
+
323
+ IntPtr hWnd2 = FindWindow("#32770", "ファイルのダウンロード");
324
+
325
+ if (hWnd2 != IntPtr.Zero)//
326
+
327
+ {
328
+
329
+ const uint WM_LBUTTONDOWN1 = 0x0201;
330
+
331
+ const uint WM_LBUTTONUP1 = 0x0202;
332
+
333
+ IntPtr hChild1;
334
+
335
+ hChild1 = IntPtr.Zero;
336
+
337
+ hChild1 = FindWindowEx(hWnd2, IntPtr.Zero, "Button", "保存(&S)");
338
+
339
+ // Saveボタンを押す
340
+
341
+ PostMessage(hChild1, WM_LBUTTONDOWN1, IntPtr.Zero, IntPtr.Zero);
342
+
343
+ PostMessage(hChild1, WM_LBUTTONUP1, IntPtr.Zero, IntPtr.Zero);
344
+
345
+ }
346
+
347
+ }
348
+
349
+ //「名前をつけて保存」の処理
350
+
351
+ private void MyClock3(object sender, EventArgs e)
352
+
353
+ {
354
+
355
+ IntPtr hWnd3 = FindWindow("#32770", "名前を付けて保存");
356
+
357
+ if (hWnd3 != IntPtr.Zero)//
358
+
359
+ {
360
+
361
+
362
+
363
+ String now = DateTime.Now.ToString("yyyyMMddHHmmss");
364
+
365
+ string filepath = (@label6.Text + "\" + now + ".txt"); // 保存先
366
+
367
+ const uint WM_SETTEXT = 0x000c;
368
+
369
+ const uint WM_LBUTTONDOWN = 0x0201;
370
+
371
+ const uint WM_LBUTTONUP = 0x0202;
372
+
373
+ IntPtr hChild;
374
+
375
+ IntPtr hEdit;
376
+
377
+
378
+
379
+ hChild = FindWindowEx(hWnd3, IntPtr.Zero, "DUIViewWndClassName", null);
380
+
381
+ hChild = FindWindowEx(hChild, IntPtr.Zero, "DirectUIHWND", null);
382
+
383
+ hChild = FindWindowEx(hChild, IntPtr.Zero, "FloatNotifySink", null);
384
+
385
+ hChild = FindWindowEx(hChild, IntPtr.Zero, "ComboBox", null);
386
+
387
+
388
+
389
+ hEdit = FindWindowEx(hChild, IntPtr.Zero, "Edit", null);
390
+
391
+ // ファイル名を設定する
392
+
393
+ SendMessage(hChild, WM_SETTEXT, IntPtr.Zero, filepath);
394
+
395
+ System.Threading.Thread.Sleep(2000);
396
+
397
+ // Saveボタンを見つける
398
+
399
+ hChild = IntPtr.Zero;
400
+
401
+ hChild = FindWindowEx(hWnd3, IntPtr.Zero, "Button", "保存(&S)");
402
+
403
+ // Saveボタンを押す
404
+
405
+ PostMessage(hChild, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
406
+
407
+ PostMessage(hChild, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
408
+
409
+ }
410
+
411
+ }
412
+
413
+ //「ダウンロードの完了」を閉じる
414
+
415
+ private void MyClock4(object sender, EventArgs e)
416
+
417
+ {
418
+
419
+ IntPtr hWnd4 = FindWindow("#32770", "ダウンロードの完了");
420
+
421
+ if (hWnd4 != IntPtr.Zero)
422
+
423
+ {
424
+
425
+ const uint WM_LBUTTONDOWN4 = 0x0201;
426
+
427
+  const uint WM_LBUTTONUP4 = 0x0202;
428
+
429
+ IntPtr hChild4;
430
+
431
+ hChild4 = IntPtr.Zero;
432
+
433
+ hChild4 = FindWindowEx(hWnd4, IntPtr.Zero, "Button", "閉じる");
434
+
435
+ // Saveボタンを押す
436
+
437
+ PostMessage(hChild4, WM_LBUTTONDOWN4, IntPtr.Zero, IntPtr.Zero);
438
+
439
+ PostMessage(hChild4, WM_LBUTTONUP4, IntPtr.Zero, IntPtr.Zero);
440
+
441
+
442
+
443
+ DateTime at = DateTime.Now;
444
+
445
+ at = at.AddMinutes(Decimal.ToInt32(numericUpDown1.Value));
446
+
447
+ label1.Text = "保存完了";
448
+
449
+ label1.Refresh();
450
+
451
+
452
+
453
+ }
454
+
455
+
456
+
457
+
458
+
459
+ }
460
+
461
+
462
+
463
+ private void button2_Click(object sender, EventArgs e)
464
+
465
+ {
466
+
467
+ //アプリケーションを終了させる
468
+
469
+ Application.Exit();
470
+
471
+ }
472
+
473
+
474
+
475
+ private void button3_Click(object sender, EventArgs e)
476
+
477
+ {
478
+
479
+ // FolderBrowserDialogクラスのインスタンス生成
480
+
481
+ FolderBrowserDialog fbd = new FolderBrowserDialog();
482
+
483
+
484
+
485
+ // ダイアログタイトルを設定
486
+
487
+ fbd.Description = "フォルダを選択してください";
488
+
489
+ fbd.RootFolder = Environment.SpecialFolder.Desktop;
490
+
491
+ fbd.SelectedPath = @"c:\test\";
492
+
493
+ fbd.ShowNewFolderButton = true;
494
+
495
+
496
+
497
+ if (fbd.ShowDialog() == Di
498
+
499
+ alogResult.OK)
500
+
501
+ {
502
+
503
+ string myPath = fbd.SelectedPath;
504
+
505
+ label6.Text = myPath;
506
+
507
+
508
+
509
+ }
510
+
511
+ }
512
+
513
+ }
514
+
515
+ }
516
+
517
+ ```

7

修正

2018/09/14 04:47

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- C# ソースのアドバイス希望
1
+ C# ソースコード添削希望
test CHANGED
@@ -1,13 +1 @@
1
- 初心者ながら一つのプログラムを作成してみました。「
1
+ 質問編集中です
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-

6

修正

2018/09/14 03:55

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,509 +1,13 @@
1
- 初心者ながら一つのプログラムを作成してみました。
1
+ 初心者ながら一つのプログラムを作成してみました。
2
-
3
- ソースの書き方についてこの様に記述したほうがいい
4
-
5
- こう記載すれば短縮・分かりやすソースになる
6
-
7
- 現在のソースではここが問題あるのでこう変えたほうがいい等
8
-
9
- 具体的にアドバイスをいただけないでしょうか。
10
2
 
11
3
 
12
4
 
13
- プログラムの簡単な流れとしては
14
-
15
- WEBページにログインしてログインしたら特定のダウンロードURLでファイルをダウンロードするプログラムです。
16
5
 
17
6
 
18
7
 
19
- 文字数の関係上、宣言文と一部省略しています。
20
-
21
- わかる範囲で結構ですのでアドバイスよろしくお願いいたします。
22
-
23
- ```C#
24
-
25
- public partial class Form1 : Form
26
-
27
- {
28
-
29
- public Form1()
30
-
31
- {
32
-
33
- InitializeComponent();
34
-
35
- }
36
-
37
-
38
-
39
- //3秒ごとに動くタイマー
40
-
41
- public void Timers(EventHandler eventHandler)
42
-
43
- {
44
-
45
- var timer = new System.Windows.Forms.Timer();
46
-
47
- timer.Tick += new EventHandler(eventHandler);
48
-
49
- timer.Interval = 3000;
50
-
51
- timer.Start();
52
-
53
- }
54
-
55
- //numericUpDown1 * 60秒 でループするタイマー
56
-
57
- public void loop(EventHandler eventHandler)
58
-
59
- {
60
-
61
- int intVal = Decimal.ToInt32(numericUpDown1.Value);
62
-
63
- var timer = new System.Windows.Forms.Timer();
64
-
65
- timer.Tick += new EventHandler(eventHandler);
66
-
67
- timer.Interval = intVal * 60000;
68
-
69
- timer.Start();
70
-
71
- }
72
-
73
- //メイン処理
74
-
75
- public void button1_Click(object sender, EventArgs e)
76
-
77
- {
78
-
79
- first();
80
-
81
- loop(main);
82
8
 
83
9
 
84
10
 
85
- }
86
-
87
- public void icmp()
88
-
89
- {
90
-
91
- strt:
92
-
93
- System.Threading.Thread.Sleep(1000);
94
-
95
- label1.Text = "通信確認中";
96
-
97
- label1.Refresh();
98
-
99
- Ping ins_Ping = new Ping();
100
-
101
- string host = textBox0.Text;
102
-
103
- int timeout = 1000;
104
-
105
- try
106
-
107
- {
108
-
109
- PingReply ins_PingReply = ins_Ping.Send(host, timeout);
110
-
111
- if (ins_PingReply.Status != IPStatus.Success)
112
-
113
- {
114
-
115
- label1.Text = "通信失敗";
116
-
117
- label1.Refresh();
118
-
119
- System.Threading.Thread.Sleep(5000);
120
-
121
- goto strt;
122
-
123
- }
124
-
125
- else
126
-
127
- {
128
-
129
- label1.Text = "通信成功";
130
-
131
- label1.Refresh();
132
-
133
- System.Threading.Thread.Sleep(5000);
134
-
135
- }
136
-
137
- }
138
-
139
- catch (PingException ex)
140
-
141
- {
142
-
143
- label1.Text = "通信失敗";
144
-
145
- label1.Refresh();
146
11
 
147
12
 
148
-
149
- // MessageBox.Show(ex.Message);
150
-
151
- System.Threading.Thread.Sleep(1000);
152
-
153
- goto strt;
154
-
155
- }
13
+
156
-
157
- }
158
-
159
- //初回動作用
160
-
161
- public void first()
162
-
163
- {
164
-
165
- try
166
-
167
- {
168
-
169
- //System.Windows.Forms.WebBrowser IE11に変更
170
-
171
- regKey.SetValue(strProcessName, 11001, Microsoft.Win32.RegistryValueKind.DWord);
172
-
173
- icmp();
174
-
175
-
176
-
177
- //タイマー1を設定
178
-
179
- Timers(MyClock1);
180
-
181
- label1.Text = "接続中";
182
-
183
- label1.Refresh();
184
-
185
- //WEBページ接続
186
-
187
- webBrowser1.Visible = true;
188
-
189
- webBrowser1.Navigate("https://" + textBox0.Text);
190
-
191
-
192
-
193
- //ページの読み込み完了まで待つ
194
-
195
- while (webBrowser1.IsBusy || webBrowser1.ReadyState != WebBrowserReadyState.Complete)
196
-
197
- {
198
-
199
- System.Windows.Forms.Application.DoEvents();
200
-
201
- System.Threading.Thread.Sleep(100);
202
-
203
- }
204
-
205
- //1番目フレーム内のNameを取得
206
-
207
- HtmlWindow iframe = webBrowser1.Document.Window.Frames[0];
208
-
209
- HtmlElementCollection elements = iframe.Document.All;
210
-
211
- HtmlElement id = elements.GetElementsByName("userName")[0];
212
-
213
- HtmlElement pw = elements.GetElementsByName("pwd")[0];
214
-
215
- //ID・passを入力
216
-
217
- id.InnerText = textBox1.Text;
218
-
219
- pw.InnerText = textBox2.Text;
220
-
221
-
222
-
223
- //Submitボタンをクリック
224
-
225
- HtmlElement login = elements.GetElementsByName("Submit")[0];
226
-
227
- login.InvokeMember("click");
228
-
229
-
230
-
231
- //タイマー2を設定
232
-
233
- Timers(MyClock2);
234
-
235
-
236
-
237
- //診断レポートのダウンロードページを開く
238
-
239
- webBrowser2.Visible = true;
240
-
241
- webBrowser2.Navigate("https://" + textBox0.Text + "/tech_.txt");
242
-
243
- label1.Text = "ダウンロード処理中";
244
-
245
-
246
-
247
- //タイマー3を設定
248
-
249
- Timers(MyClock3);
250
-
251
-
252
-
253
- Timers(MyClock4);
254
-
255
- }catch (Exception e)when (e is ObjectDisposedException || e is InvalidOperationException)
256
-
257
- {
258
-
259
- System.Threading.Thread.Sleep(1000);
260
-
261
- first();
262
-
263
- }
264
-
265
- }
266
-
267
- private void main(object sender, EventArgs e)
268
-
269
- {
270
-
271
- try
272
-
273
- {
274
-
275
- //firstとほぼ同じで少しいらない点を省略したものが入っています。
276
-
277
- }catch (Exception w) when (w is ObjectDisposedException || w is InvalidOperationException || w is ArgumentOutOfRangeException)
278
-
279
- {
280
-
281
- System.Threading.Thread.Sleep(1000);
282
-
283
-
284
-
285
- }
286
-
287
- }
288
-
289
-   //「セキュリティの警告」を承認する
290
-
291
- private void MyClock1(object sender, EventArgs e)
292
-
293
- {
294
-
295
- IntPtr hWnd = FindWindow("#32770", "セキュリティの警告");
296
-
297
-
298
-
299
- if (hWnd != IntPtr.Zero)//
300
-
301
- {
302
-
303
-
304
-
305
- const uint WM_LBUTTONDOWN0 = 0x0201;
306
-
307
- const uint WM_LBUTTONUP0 = 0x0202;
308
-
309
- IntPtr hChild0;
310
-
311
- hChild0 = IntPtr.Zero;
312
-
313
- hChild0 = FindWindowEx(hWnd, IntPtr.Zero, "Button", "はい(&Y)");
314
-
315
- // はい(&Y)ボタンを押す
316
-
317
- PostMessage(hChild0, WM_LBUTTONDOWN0, IntPtr.Zero, IntPtr.Zero);
318
-
319
- PostMessage(hChild0, WM_LBUTTONUP0, IntPtr.Zero, IntPtr.Zero);
320
-
321
- }
322
-
323
-
324
-
325
- }
326
-
327
- //「ファイルのダウンロード」の保存ボタンを押す
328
-
329
- private void MyClock2(object sender, EventArgs e)
330
-
331
- {
332
-
333
- IntPtr hWnd2 = FindWindow("#32770", "ファイルのダウンロード");
334
-
335
- if (hWnd2 != IntPtr.Zero)//
336
-
337
- {
338
-
339
- const uint WM_LBUTTONDOWN1 = 0x0201;
340
-
341
- const uint WM_LBUTTONUP1 = 0x0202;
342
-
343
- IntPtr hChild1;
344
-
345
- hChild1 = IntPtr.Zero;
346
-
347
- hChild1 = FindWindowEx(hWnd2, IntPtr.Zero, "Button", "保存(&S)");
348
-
349
- // Saveボタンを押す
350
-
351
- PostMessage(hChild1, WM_LBUTTONDOWN1, IntPtr.Zero, IntPtr.Zero);
352
-
353
- PostMessage(hChild1, WM_LBUTTONUP1, IntPtr.Zero, IntPtr.Zero);
354
-
355
-
356
-
357
- }
358
-
359
- }
360
-
361
- //「名前をつけて保存」の処理
362
-
363
- private void MyClock3(object sender, EventArgs e)
364
-
365
- {
366
-
367
- IntPtr hWnd3 = FindWindow("#32770", "名前を付けて保存");
368
-
369
- if (hWnd3 != IntPtr.Zero)//
370
-
371
- {
372
-
373
-
374
-
375
- String now = DateTime.Now.ToString("yyyyMMddHHmmss");
376
-
377
- string filepath = (@label6.Text + "\" + now + ".wri"); // 保存先
378
-
379
- const uint WM_SETTEXT = 0x000c;
380
-
381
- const uint WM_LBUTTONDOWN = 0x0201;
382
-
383
- const uint WM_LBUTTONUP = 0x0202;
384
-
385
- IntPtr hChild;
386
-
387
- IntPtr hEdit;
388
-
389
-
390
-
391
- hChild = FindWindowEx(hWnd3, IntPtr.Zero, "DUIViewWndClassName", null);
392
-
393
- hChild = FindWindowEx(hChild, IntPtr.Zero, "DirectUIHWND", null);
394
-
395
- hChild = FindWindowEx(hChild, IntPtr.Zero, "FloatNotifySink", null);
396
-
397
- hChild = FindWindowEx(hChild, IntPtr.Zero, "ComboBox", null);
398
-
399
-
400
-
401
- hEdit = FindWindowEx(hChild, IntPtr.Zero, "Edit", null);
402
-
403
- SendMessage(hChild, WM_SETTEXT, IntPtr.Zero, filepath);
404
-
405
- System.Threading.Thread.Sleep(2000);
406
-
407
- hChild = IntPtr.Zero;
408
-
409
- hChild = FindWindowEx(hWnd3, IntPtr.Zero, "Button", "保存(&S)");
410
-
411
- PostMessage(hChild, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
412
-
413
- PostMessage(hChild, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
414
-
415
- }
416
-
417
- }
418
-
419
- //「ダウンロードの完了」を閉じる
420
-
421
- private void MyClock4(object sender, EventArgs e)
422
-
423
- {
424
-
425
- IntPtr hWnd4 = FindWindow("#32770", "ダウンロードの完了");
426
-
427
- if (hWnd4 != IntPtr.Zero)
428
-
429
- {
430
-
431
- const uint WM_LBUTTONDOWN4 = 0x0201;
432
-
433
-  const uint WM_LBUTTONUP4 = 0x0202;
434
-
435
- IntPtr hChild4;
436
-
437
- hChild4 = IntPtr.Zero;
438
-
439
- hChild4 = FindWindowEx(hWnd4, IntPtr.Zero, "Button", "閉じる");
440
-
441
- // Saveボタンを押す
442
-
443
- PostMessage(hChild4, WM_LBUTTONDOWN4, IntPtr.Zero, IntPtr.Zero);
444
-
445
- PostMessage(hChild4, WM_LBUTTONUP4, IntPtr.Zero, IntPtr.Zero);
446
-
447
-
448
-
449
- DateTime at = DateTime.Now;
450
-
451
- at = at.AddMinutes(Decimal.ToInt32(numericUpDown1.Value));
452
-
453
- label1.Text = "保存完了-次回は" + at.Hour + "時" + at.Minute + "分" + at.Second + "秒に開始";
454
-
455
- label1.Refresh();
456
-
457
- }
458
-
459
- }
460
-
461
- private void button2_Click(object sender, EventArgs e)
462
-
463
- {
464
-
465
- //アプリケーションを終了させる
466
-
467
- Application.Exit();
468
-
469
- }
470
-
471
- //保存先ダイアログの表示
472
-
473
- private void button3_Click(object sender, EventArgs e)
474
-
475
- {
476
-
477
- // FolderBrowserDialog
478
-
479
- FolderBrowserDialog fbd = new FolderBrowserDialog();
480
-
481
-
482
-
483
- // ダイアログタイトルを設定
484
-
485
- fbd.Description = "フォルダを選択してください";
486
-
487
- fbd.RootFolder = Environment.SpecialFolder.Desktop;
488
-
489
- fbd.SelectedPath = @"c:\test\txt";
490
-
491
- fbd.ShowNewFolderButton = true;
492
-
493
-
494
-
495
- if (fbd.ShowDialog() == DialogResult.OK)
496
-
497
- {
498
-
499
- string myPath = fbd.SelectedPath;
500
-
501
- label6.Text = myPath;
502
-
503
- }
504
-
505
- }
506
-
507
- }
508
-
509
- ```

5

変更

2018/09/14 03:51

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -486,7 +486,7 @@
486
486
 
487
487
  fbd.RootFolder = Environment.SpecialFolder.Desktop;
488
488
 
489
- fbd.SelectedPath = @"c:\test\wri";
489
+ fbd.SelectedPath = @"c:\test\txt";
490
490
 
491
491
  fbd.ShowNewFolderButton = true;
492
492
 

4

変更

2018/09/13 06:51

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -178,7 +178,7 @@
178
178
 
179
179
  Timers(MyClock1);
180
180
 
181
- label1.Text = "SonicWALL接続中";
181
+ label1.Text = "接続中";
182
182
 
183
183
  label1.Refresh();
184
184
 

3

題名の変更

2018/09/13 06:49

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- C# HTTPClientでPOSTを送信する方法
1
+ C# ソースのアドバイス希望
test CHANGED
File without changes

2

内容追加

2018/09/13 06:30

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -14,6 +14,12 @@
14
14
 
15
15
  WEBページにログインしてログインしたら特定のダウンロードURLでファイルをダウンロードするプログラムです。
16
16
 
17
+
18
+
19
+ 文字数の関係上、宣言文と一部省略しています。
20
+
21
+ わかる範囲で結構ですのでアドバイスよろしくお願いいたします。
22
+
17
23
  ```C#
18
24
 
19
25
  public partial class Form1 : Form

1

追加

2018/09/13 06:17

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -13,3 +13,491 @@
13
13
  プログラムの簡単な流れとしては
14
14
 
15
15
  WEBページにログインしてログインしたら特定のダウンロードURLでファイルをダウンロードするプログラムです。
16
+
17
+ ```C#
18
+
19
+ public partial class Form1 : Form
20
+
21
+ {
22
+
23
+ public Form1()
24
+
25
+ {
26
+
27
+ InitializeComponent();
28
+
29
+ }
30
+
31
+
32
+
33
+ //3秒ごとに動くタイマー
34
+
35
+ public void Timers(EventHandler eventHandler)
36
+
37
+ {
38
+
39
+ var timer = new System.Windows.Forms.Timer();
40
+
41
+ timer.Tick += new EventHandler(eventHandler);
42
+
43
+ timer.Interval = 3000;
44
+
45
+ timer.Start();
46
+
47
+ }
48
+
49
+ //numericUpDown1 * 60秒 でループするタイマー
50
+
51
+ public void loop(EventHandler eventHandler)
52
+
53
+ {
54
+
55
+ int intVal = Decimal.ToInt32(numericUpDown1.Value);
56
+
57
+ var timer = new System.Windows.Forms.Timer();
58
+
59
+ timer.Tick += new EventHandler(eventHandler);
60
+
61
+ timer.Interval = intVal * 60000;
62
+
63
+ timer.Start();
64
+
65
+ }
66
+
67
+ //メイン処理
68
+
69
+ public void button1_Click(object sender, EventArgs e)
70
+
71
+ {
72
+
73
+ first();
74
+
75
+ loop(main);
76
+
77
+
78
+
79
+ }
80
+
81
+ public void icmp()
82
+
83
+ {
84
+
85
+ strt:
86
+
87
+ System.Threading.Thread.Sleep(1000);
88
+
89
+ label1.Text = "通信確認中";
90
+
91
+ label1.Refresh();
92
+
93
+ Ping ins_Ping = new Ping();
94
+
95
+ string host = textBox0.Text;
96
+
97
+ int timeout = 1000;
98
+
99
+ try
100
+
101
+ {
102
+
103
+ PingReply ins_PingReply = ins_Ping.Send(host, timeout);
104
+
105
+ if (ins_PingReply.Status != IPStatus.Success)
106
+
107
+ {
108
+
109
+ label1.Text = "通信失敗";
110
+
111
+ label1.Refresh();
112
+
113
+ System.Threading.Thread.Sleep(5000);
114
+
115
+ goto strt;
116
+
117
+ }
118
+
119
+ else
120
+
121
+ {
122
+
123
+ label1.Text = "通信成功";
124
+
125
+ label1.Refresh();
126
+
127
+ System.Threading.Thread.Sleep(5000);
128
+
129
+ }
130
+
131
+ }
132
+
133
+ catch (PingException ex)
134
+
135
+ {
136
+
137
+ label1.Text = "通信失敗";
138
+
139
+ label1.Refresh();
140
+
141
+
142
+
143
+ // MessageBox.Show(ex.Message);
144
+
145
+ System.Threading.Thread.Sleep(1000);
146
+
147
+ goto strt;
148
+
149
+ }
150
+
151
+ }
152
+
153
+ //初回動作用
154
+
155
+ public void first()
156
+
157
+ {
158
+
159
+ try
160
+
161
+ {
162
+
163
+ //System.Windows.Forms.WebBrowser IE11に変更
164
+
165
+ regKey.SetValue(strProcessName, 11001, Microsoft.Win32.RegistryValueKind.DWord);
166
+
167
+ icmp();
168
+
169
+
170
+
171
+ //タイマー1を設定
172
+
173
+ Timers(MyClock1);
174
+
175
+ label1.Text = "SonicWALL接続中";
176
+
177
+ label1.Refresh();
178
+
179
+ //WEBページ接続
180
+
181
+ webBrowser1.Visible = true;
182
+
183
+ webBrowser1.Navigate("https://" + textBox0.Text);
184
+
185
+
186
+
187
+ //ページの読み込み完了まで待つ
188
+
189
+ while (webBrowser1.IsBusy || webBrowser1.ReadyState != WebBrowserReadyState.Complete)
190
+
191
+ {
192
+
193
+ System.Windows.Forms.Application.DoEvents();
194
+
195
+ System.Threading.Thread.Sleep(100);
196
+
197
+ }
198
+
199
+ //1番目フレーム内のNameを取得
200
+
201
+ HtmlWindow iframe = webBrowser1.Document.Window.Frames[0];
202
+
203
+ HtmlElementCollection elements = iframe.Document.All;
204
+
205
+ HtmlElement id = elements.GetElementsByName("userName")[0];
206
+
207
+ HtmlElement pw = elements.GetElementsByName("pwd")[0];
208
+
209
+ //ID・passを入力
210
+
211
+ id.InnerText = textBox1.Text;
212
+
213
+ pw.InnerText = textBox2.Text;
214
+
215
+
216
+
217
+ //Submitボタンをクリック
218
+
219
+ HtmlElement login = elements.GetElementsByName("Submit")[0];
220
+
221
+ login.InvokeMember("click");
222
+
223
+
224
+
225
+ //タイマー2を設定
226
+
227
+ Timers(MyClock2);
228
+
229
+
230
+
231
+ //診断レポートのダウンロードページを開く
232
+
233
+ webBrowser2.Visible = true;
234
+
235
+ webBrowser2.Navigate("https://" + textBox0.Text + "/tech_.txt");
236
+
237
+ label1.Text = "ダウンロード処理中";
238
+
239
+
240
+
241
+ //タイマー3を設定
242
+
243
+ Timers(MyClock3);
244
+
245
+
246
+
247
+ Timers(MyClock4);
248
+
249
+ }catch (Exception e)when (e is ObjectDisposedException || e is InvalidOperationException)
250
+
251
+ {
252
+
253
+ System.Threading.Thread.Sleep(1000);
254
+
255
+ first();
256
+
257
+ }
258
+
259
+ }
260
+
261
+ private void main(object sender, EventArgs e)
262
+
263
+ {
264
+
265
+ try
266
+
267
+ {
268
+
269
+ //firstとほぼ同じで少しいらない点を省略したものが入っています。
270
+
271
+ }catch (Exception w) when (w is ObjectDisposedException || w is InvalidOperationException || w is ArgumentOutOfRangeException)
272
+
273
+ {
274
+
275
+ System.Threading.Thread.Sleep(1000);
276
+
277
+
278
+
279
+ }
280
+
281
+ }
282
+
283
+   //「セキュリティの警告」を承認する
284
+
285
+ private void MyClock1(object sender, EventArgs e)
286
+
287
+ {
288
+
289
+ IntPtr hWnd = FindWindow("#32770", "セキュリティの警告");
290
+
291
+
292
+
293
+ if (hWnd != IntPtr.Zero)//
294
+
295
+ {
296
+
297
+
298
+
299
+ const uint WM_LBUTTONDOWN0 = 0x0201;
300
+
301
+ const uint WM_LBUTTONUP0 = 0x0202;
302
+
303
+ IntPtr hChild0;
304
+
305
+ hChild0 = IntPtr.Zero;
306
+
307
+ hChild0 = FindWindowEx(hWnd, IntPtr.Zero, "Button", "はい(&Y)");
308
+
309
+ // はい(&Y)ボタンを押す
310
+
311
+ PostMessage(hChild0, WM_LBUTTONDOWN0, IntPtr.Zero, IntPtr.Zero);
312
+
313
+ PostMessage(hChild0, WM_LBUTTONUP0, IntPtr.Zero, IntPtr.Zero);
314
+
315
+ }
316
+
317
+
318
+
319
+ }
320
+
321
+ //「ファイルのダウンロード」の保存ボタンを押す
322
+
323
+ private void MyClock2(object sender, EventArgs e)
324
+
325
+ {
326
+
327
+ IntPtr hWnd2 = FindWindow("#32770", "ファイルのダウンロード");
328
+
329
+ if (hWnd2 != IntPtr.Zero)//
330
+
331
+ {
332
+
333
+ const uint WM_LBUTTONDOWN1 = 0x0201;
334
+
335
+ const uint WM_LBUTTONUP1 = 0x0202;
336
+
337
+ IntPtr hChild1;
338
+
339
+ hChild1 = IntPtr.Zero;
340
+
341
+ hChild1 = FindWindowEx(hWnd2, IntPtr.Zero, "Button", "保存(&S)");
342
+
343
+ // Saveボタンを押す
344
+
345
+ PostMessage(hChild1, WM_LBUTTONDOWN1, IntPtr.Zero, IntPtr.Zero);
346
+
347
+ PostMessage(hChild1, WM_LBUTTONUP1, IntPtr.Zero, IntPtr.Zero);
348
+
349
+
350
+
351
+ }
352
+
353
+ }
354
+
355
+ //「名前をつけて保存」の処理
356
+
357
+ private void MyClock3(object sender, EventArgs e)
358
+
359
+ {
360
+
361
+ IntPtr hWnd3 = FindWindow("#32770", "名前を付けて保存");
362
+
363
+ if (hWnd3 != IntPtr.Zero)//
364
+
365
+ {
366
+
367
+
368
+
369
+ String now = DateTime.Now.ToString("yyyyMMddHHmmss");
370
+
371
+ string filepath = (@label6.Text + "\" + now + ".wri"); // 保存先
372
+
373
+ const uint WM_SETTEXT = 0x000c;
374
+
375
+ const uint WM_LBUTTONDOWN = 0x0201;
376
+
377
+ const uint WM_LBUTTONUP = 0x0202;
378
+
379
+ IntPtr hChild;
380
+
381
+ IntPtr hEdit;
382
+
383
+
384
+
385
+ hChild = FindWindowEx(hWnd3, IntPtr.Zero, "DUIViewWndClassName", null);
386
+
387
+ hChild = FindWindowEx(hChild, IntPtr.Zero, "DirectUIHWND", null);
388
+
389
+ hChild = FindWindowEx(hChild, IntPtr.Zero, "FloatNotifySink", null);
390
+
391
+ hChild = FindWindowEx(hChild, IntPtr.Zero, "ComboBox", null);
392
+
393
+
394
+
395
+ hEdit = FindWindowEx(hChild, IntPtr.Zero, "Edit", null);
396
+
397
+ SendMessage(hChild, WM_SETTEXT, IntPtr.Zero, filepath);
398
+
399
+ System.Threading.Thread.Sleep(2000);
400
+
401
+ hChild = IntPtr.Zero;
402
+
403
+ hChild = FindWindowEx(hWnd3, IntPtr.Zero, "Button", "保存(&S)");
404
+
405
+ PostMessage(hChild, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
406
+
407
+ PostMessage(hChild, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
408
+
409
+ }
410
+
411
+ }
412
+
413
+ //「ダウンロードの完了」を閉じる
414
+
415
+ private void MyClock4(object sender, EventArgs e)
416
+
417
+ {
418
+
419
+ IntPtr hWnd4 = FindWindow("#32770", "ダウンロードの完了");
420
+
421
+ if (hWnd4 != IntPtr.Zero)
422
+
423
+ {
424
+
425
+ const uint WM_LBUTTONDOWN4 = 0x0201;
426
+
427
+  const uint WM_LBUTTONUP4 = 0x0202;
428
+
429
+ IntPtr hChild4;
430
+
431
+ hChild4 = IntPtr.Zero;
432
+
433
+ hChild4 = FindWindowEx(hWnd4, IntPtr.Zero, "Button", "閉じる");
434
+
435
+ // Saveボタンを押す
436
+
437
+ PostMessage(hChild4, WM_LBUTTONDOWN4, IntPtr.Zero, IntPtr.Zero);
438
+
439
+ PostMessage(hChild4, WM_LBUTTONUP4, IntPtr.Zero, IntPtr.Zero);
440
+
441
+
442
+
443
+ DateTime at = DateTime.Now;
444
+
445
+ at = at.AddMinutes(Decimal.ToInt32(numericUpDown1.Value));
446
+
447
+ label1.Text = "保存完了-次回は" + at.Hour + "時" + at.Minute + "分" + at.Second + "秒に開始";
448
+
449
+ label1.Refresh();
450
+
451
+ }
452
+
453
+ }
454
+
455
+ private void button2_Click(object sender, EventArgs e)
456
+
457
+ {
458
+
459
+ //アプリケーションを終了させる
460
+
461
+ Application.Exit();
462
+
463
+ }
464
+
465
+ //保存先ダイアログの表示
466
+
467
+ private void button3_Click(object sender, EventArgs e)
468
+
469
+ {
470
+
471
+ // FolderBrowserDialog
472
+
473
+ FolderBrowserDialog fbd = new FolderBrowserDialog();
474
+
475
+
476
+
477
+ // ダイアログタイトルを設定
478
+
479
+ fbd.Description = "フォルダを選択してください";
480
+
481
+ fbd.RootFolder = Environment.SpecialFolder.Desktop;
482
+
483
+ fbd.SelectedPath = @"c:\test\wri";
484
+
485
+ fbd.ShowNewFolderButton = true;
486
+
487
+
488
+
489
+ if (fbd.ShowDialog() == DialogResult.OK)
490
+
491
+ {
492
+
493
+ string myPath = fbd.SelectedPath;
494
+
495
+ label6.Text = myPath;
496
+
497
+ }
498
+
499
+ }
500
+
501
+ }
502
+
503
+ ```