teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

14

不正削除ロールバック

2018/09/20 00:35

投稿

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

13

2018/09/20 00:35

投稿

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

12

修正

2018/09/19 03:16

投稿

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

11

題名変更

2018/09/18 06:46

投稿

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

10

変更

2018/09/14 07:45

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -5,7 +5,7 @@
5
5
  {
6
6
  public Form1()
7
7
  {
8
- InitializeComponent();
8
+ InitializeComponent(); 
9
9
  }
10
10
 
11
11
  //3秒ごとに動くタイマー
@@ -81,7 +81,7 @@
81
81
 
82
82
  //タイマー1を設定
83
83
  Timers(MyClock1);
84
- label1.Text = "SonicWALL接続中";
84
+ label1.Text = "接続中";
85
85
  label1.Refresh();
86
86
  //接続
87
87
  //ブラウザのサイズを変更する
@@ -110,7 +110,7 @@
110
110
  //タイマー2を設定
111
111
  Timers(MyClock2);
112
112
 
113
- //診断レポートのダウンロードページを開く
113
+ //ダウンロードページを開く
114
114
  webBrowser2.Visible = true;
115
115
  webBrowser2.Navigate("https://" + textBox0.Text + "/techSupport_.wri");
116
116
  label1.Text = "ダウンロード処理中";

9

ソース

2018/09/14 04:52

投稿

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

8

ソース

2018/09/14 04:50

投稿

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

7

修正

2018/09/14 04:47

投稿

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

6

修正

2018/09/14 03:55

投稿

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

5

変更

2018/09/14 03:51

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -242,7 +242,7 @@
242
242
  // ダイアログタイトルを設定
243
243
  fbd.Description = "フォルダを選択してください";
244
244
  fbd.RootFolder = Environment.SpecialFolder.Desktop;
245
- fbd.SelectedPath = @"c:\test\wri";
245
+ fbd.SelectedPath = @"c:\test\txt";
246
246
  fbd.ShowNewFolderButton = true;
247
247
 
248
248
  if (fbd.ShowDialog() == DialogResult.OK)

4

変更

2018/09/13 06:51

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -88,7 +88,7 @@
88
88
 
89
89
  //タイマー1を設定
90
90
  Timers(MyClock1);
91
- label1.Text = "SonicWALL接続中";
91
+ label1.Text = "接続中";
92
92
  label1.Refresh();
93
93
  //WEBページ接続
94
94
  webBrowser1.Visible = true;

3

題名の変更

2018/09/13 06:49

投稿

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

2

内容追加

2018/09/13 06:30

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -6,6 +6,9 @@
6
6
 
7
7
  プログラムの簡単な流れとしては
8
8
  WEBページにログインしてログインしたら特定のダウンロードURLでファイルをダウンロードするプログラムです。
9
+
10
+ 文字数の関係上、宣言文と一部省略しています。
11
+ わかる範囲で結構ですのでアドバイスよろしくお願いいたします。
9
12
  ```C#
10
13
  public partial class Form1 : Form
11
14
  {

1

追加

2018/09/13 06:17

投稿

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