1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using EnergyCollector.Frame;
10 using System.IO;
11
12 namespace EnergyCollector.Winform
13 {
14 public partial class FrmImExConfigFile : BackGroundForm
15 {
16 public const int WM_DEVICECHANGE = 0x219;
17 public const int DBT_DEVICEARRIVAL = 0x8000;
18 public const int DBT_CONFIGCHANGECANCELED = 0x0019;
19 public const int DBT_CONFIGCHANGED = 0x0018;
20 public const int DBT_CUSTOMEVENT = 0x8006;
21 public const int DBT_DEVICEQUERYREMOVE = 0x8001;
22 public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
23 public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
24 public const int DBT_DEVICEREMOVEPENDING = 0x8003;
25 public const int DBT_DEVICETYPESPECIFIC = 0x8005;
26 public const int DBT_DEVNODES_CHANGED = 0x0007;
27 public const int DBT_QUERYCHANGECONFIG = 0x0017;
28 public const int DBT_USERDEFINED = 0xFFFF;
29 /// <summary>
30 /// 盘符
31 /// </summary>
32 private string driveName;
33
34 public FrmImExConfigFile()
35 {
36 InitializeComponent();
37 }
38
39 protected override void WndProc(ref Message m)
40 {
41 try
42 {
43 if (m.Msg == WM_DEVICECHANGE)
44 {
45 switch (m.WParam.ToInt32())
46 {
47 case WM_DEVICECHANGE://
48 break;
49 case DBT_DEVICEARRIVAL://U盘插入
50 DriveInfo[] s = DriveInfo.GetDrives();
51 foreach (DriveInfo drive in s)
52 {
53 if (drive.DriveType == DriveType.Removable)
54 {
55 richTextBox1.AppendText("U盘已插入! \r\n");
56 richTextBox1.AppendText("-----------\r\n");
57 driveName = drive.Name;
58 kbExport.Enabled = true;
59 kbImport.Enabled = true;
60 break;
61 }
62 }
63 break;
64 case DBT_CONFIGCHANGECANCELED:
65 richTextBox1.AppendText("2\r\n");
66 break;
67 case DBT_CONFIGCHANGED:
68 richTextBox1.AppendText("3\r\n");
69 break;
70 case DBT_CUSTOMEVENT:
71 richTextBox1.AppendText("4\r\n");
72 break;
73 case DBT_DEVICEQUERYREMOVE:
74 richTextBox1.AppendText("5\r\n");
75 break;
76 case DBT_DEVICEQUERYREMOVEFAILED:
77 richTextBox1.AppendText("6\r\n");
78 break;
79 case DBT_DEVICEREMOVECOMPLETE: //U盘卸载
80 richTextBox1.AppendText("U盘已卸载! \r\n");
81 richTextBox1.AppendText("-----------\r\n");
82 kbExport.Enabled = false;
83 kbImport.Enabled = false;
84 break;
85 case DBT_DEVICEREMOVEPENDING:
86 richTextBox1.AppendText("7\r\n");
87 break;
88 case DBT_DEVICETYPESPECIFIC:
89 richTextBox1.AppendText("8\r\n");
90 break;
91 case DBT_DEVNODES_CHANGED://可用,设备变化时
92 //richTextBox1.AppendText("9\r\n");
93 break;
94 case DBT_QUERYCHANGECONFIG:
95 richTextBox1.AppendText("10\r\n");
96 break;
97 case DBT_USERDEFINED:
98 richTextBox1.AppendText("11\r\n");
99 break;
100 default:
101 break;
102 }
103 }
104 }
105 catch (Exception ex)
106 {
107 richTextBox1.AppendText(ex.Message);
108 }
109 base.WndProc(ref m);
110 }
111 /// <summary>
112 /// 加载
113 /// </summary>
114 /// <param name="sender"></param>
115 /// <param name="e"></param>
116 private void FrmImExConfigFile_Load(object sender, EventArgs e)
117 {
118 kbExport.Enabled = false;
119 kbImport.Enabled = false;
120 richTextBox1.AppendText("请插入U盘! \r\n");
121 richTextBox1.AppendText("-----------\r\n");
122 }
123 /// <summary>
124 /// 导出配置文件
125 /// </summary>
126 /// <param name="sender"></param>
127 /// <param name="e"></param>
128 private void kbExport_Click(object sender, EventArgs e)
129 {
130 //程序路径
131 string localPath = System.Environment.CurrentDirectory + "\\ConfigFiles";
132 //目标路径
133 string targetPath = driveName + "ConfigFiles";
134 //判断路径是否存在
135 if (!Directory.Exists(localPath))
136 {
137 richTextBox1.AppendText("配置目录不存在!\r\n");
138 return;
139 }
140 if (!Directory.Exists(driveName))
141 {
142 richTextBox1.AppendText("U盘不存在!\r\n");
143 return;
144 }
145 //实例化
146 DirectoryInfo dirLocal = new DirectoryInfo(localPath);
147 DirectoryInfo dirTarget = new DirectoryInfo(targetPath);
148 //判断目标路径是否存在
149 if (!Directory.Exists(targetPath))
150 {
151 Directory.CreateDirectory(targetPath);
152 }
153 //删除历史文件
154 if (dirTarget.GetFiles().Length > 0)
155 {
156 richTextBox1.AppendText("正在删除U盘历史文件");
157 foreach (FileInfo file in dirTarget.GetFiles())
158 {
159 file.Delete();
160 richTextBox1.AppendText(".");
161 }
162 }
163 richTextBox1.AppendText("\r\n");
164 //复制配置文件
165 richTextBox1.AppendText("正在导出配置文件到U盘");
166 foreach (FileInfo file in dirLocal.GetFiles())
167 {
168 string fileNew = targetPath + "\\" + file.Name;
169 file.CopyTo(fileNew, true);
170 richTextBox1.AppendText(".");
171 }
172 richTextBox1.AppendText("\r\n");
173 richTextBox1.AppendText("导出完成!\r\n");
174 richTextBox1.AppendText("-----------\r\n");
175 }
176 /// <summary>
177 /// 导入配置文件
178 /// </summary>
179 /// <param name="sender"></param>
180 /// <param name="e"></param>
181 private void kbImport_Click(object sender, EventArgs e)
182 {
183 //程序路径
184 string localPath = System.Environment.CurrentDirectory + "\\ConfigFiles";
185 //目标路径
186 string targetPath = driveName + "ConfigFiles";
187 //判断路径是否存在
188 if (!Directory.Exists(localPath))
189 {
190 richTextBox1.AppendText("配置目录不存在!\r\n");
191 return;
192 }
193 if (!Directory.Exists(driveName))
194 {
195 richTextBox1.AppendText("U盘不存在!\r\n");
196 return;
197 }
198 //实例化
199 DirectoryInfo dirLocal = new DirectoryInfo(localPath);
200 DirectoryInfo dirTarget = new DirectoryInfo(targetPath);
201 //判断目标路径是否存在
202 if (!Directory.Exists(targetPath))
203 {
204 Directory.CreateDirectory(targetPath);
205 }
206 if (dirTarget.GetFiles().Length > 0)
207 {
208 //删除历史文件
209 if (dirLocal.GetFiles().Length > 0)
210 {
211 richTextBox1.AppendText("正在删除本地配置");
212 foreach (FileInfo file in dirLocal.GetFiles())
213 {
214 file.Delete();
215 richTextBox1.AppendText(".");
216 }
217 }
218 richTextBox1.AppendText("\r\n");
219 //复制配置文件
220 richTextBox1.AppendText("正在导入配置文件到本机");
221 foreach (FileInfo file in dirTarget.GetFiles())
222 {
223 string fileNew = dirLocal + "\\" + file.Name;
224 file.CopyTo(fileNew, true);
225 richTextBox1.AppendText(".");
226 }
227 richTextBox1.AppendText("\r\n");
228 richTextBox1.AppendText("导入完成!\r\n");
229 }
230 else
231 {
232 richTextBox1.AppendText("U盘配置文件为空,请检查配置文件!\r\n");
233 }
234 richTextBox1.AppendText("-----------\r\n");
235
236 }
237
238 private void kbClearMsg_Click(object sender, EventArgs e)
239 {
240 richTextBox1.Clear();
241 }
242
243 private void keyButtonCancel_Click(object sender, EventArgs e)
244 {
245 this.Close();
246 }
247
248
249 }
250 }
U盘是否插入
U盘是否插入+导入导出文件
时间: 2024-09-28 13:41:49