root / HAgent / Horizon(TCP, HTTP) / Horizon / ContentCheck.xaml.cs
이력 | 보기 | 이력해설 | 다운로드 (10.6 KB)
1 | 40 | HKM | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.Globalization; |
||
4 | using System.IO; |
||
5 | using System.Net; |
||
6 | using System.Windows; |
||
7 | using System.Windows.Input; |
||
8 | using System.Xml; |
||
9 | using System.Runtime.InteropServices; |
||
10 | using System.Text; |
||
11 | |||
12 | namespace Horizon |
||
13 | { |
||
14 | public partial class ContentCheck : Window |
||
15 | { |
||
16 | private const int listenPort = 11000; |
||
17 | |||
18 | int needUpdateCount; |
||
19 | |||
20 | string defaultPath = @"Program\"; |
||
21 | string url; |
||
22 | string project; |
||
23 | string ServerIP; |
||
24 | string ServerPort; |
||
25 | |||
26 | [DllImport("kernel32")] |
||
27 | private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); |
||
28 | [DllImport("kernel32")] |
||
29 | private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); |
||
30 | |||
31 | static string FTPDataName; |
||
32 | static string FTPDataTime; |
||
33 | |||
34 | static string LocalDataName; |
||
35 | static string LocalDataTime; |
||
36 | |||
37 | static string FTPDataInfo; |
||
38 | |||
39 | public void XmlReader() |
||
40 | { |
||
41 | XmlDocument xml = new XmlDocument(); |
||
42 | xml.Load("Setting.xml"); |
||
43 | XmlElement root = xml.DocumentElement; |
||
44 | |||
45 | project = root.GetAttribute("ServerInfo"); |
||
46 | ServerIP = root.GetAttribute("IP"); |
||
47 | ServerPort = root.GetAttribute("PORT"); |
||
48 | } |
||
49 | |||
50 | public void CheckDirectory()// Program 디렉토리 생성 |
||
51 | { |
||
52 | if (!Directory.Exists("Program")) |
||
53 | { |
||
54 | Directory.CreateDirectory("Program"); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | static public void IniLogSave(string section, string key ,string value) |
||
59 | { |
||
60 | WritePrivateProfileString(section, "Location", section.Substring(section.LastIndexOf('/') + 1, section.Length - section.LastIndexOf('/') - 1), System.Environment.CurrentDirectory +"/" + "UpdateLog.ini"); |
||
61 | WritePrivateProfileString(section, "Date", value , System.Environment.CurrentDirectory + "/" + "UpdateLog.ini"); |
||
62 | FTPDataName = section; |
||
63 | FTPDataTime = value; |
||
64 | } |
||
65 | |||
66 | |||
67 | static public void IniLogLoad(string section, string value) |
||
68 | { |
||
69 | StringBuilder DateTemp = new StringBuilder(255); |
||
70 | StringBuilder LocationTemp = new StringBuilder(255); |
||
71 | |||
72 | int ret = GetPrivateProfileString(section, "Date", "", DateTemp, 255, System.Environment.CurrentDirectory + "/" + "UpdateLog.ini"); |
||
73 | int ret2 = GetPrivateProfileString(section, "Location", "", LocationTemp, 255 , System.Environment.CurrentDirectory + "/" + "UpdateLog.ini"); |
||
74 | |||
75 | LocalDataTime = DateTemp.ToString(); |
||
76 | LocalDataName = LocationTemp.ToString(); |
||
77 | } |
||
78 | |||
79 | public ContentCheck() |
||
80 | { |
||
81 | // IniLogLoad(); |
||
82 | InitializeComponent(); |
||
83 | |||
84 | CheckDirectory(); |
||
85 | |||
86 | GetFTPList(host, user, pass); |
||
87 | } |
||
88 | private void ExitButton(object sender, MouseButtonEventArgs e) |
||
89 | { |
||
90 | Close(); |
||
91 | } |
||
92 | |||
93 | private void Minimization(object sender, MouseButtonEventArgs e) |
||
94 | { |
||
95 | this.WindowState = WindowState.Minimized; |
||
96 | } |
||
97 | |||
98 | private static string host = "ftp://192.168.0.61:11000/"; |
||
99 | private static string user = "user"; |
||
100 | private static string pass = "1234"; |
||
101 | private static FtpWebRequest ftpRequest = null; |
||
102 | |||
103 | private static FtpWebResponse ftpResponse = null; |
||
104 | private static Stream ftpStream = null; |
||
105 | private static int bufferSize = 2048; |
||
106 | |||
107 | /* Construct Object */ |
||
108 | public void SetClient(string hostIP, string userName, string password) |
||
109 | { |
||
110 | host = hostIP; |
||
111 | user = userName; |
||
112 | pass = password; |
||
113 | } |
||
114 | |||
115 | /* Download File */ |
||
116 | static public void download(string remoteFile, string localFile) |
||
117 | { |
||
118 | try |
||
119 | { |
||
120 | ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile); |
||
121 | ftpRequest.Credentials = new NetworkCredential(user, pass); |
||
122 | ftpRequest.UseBinary = true; |
||
123 | ftpRequest.UsePassive = true; |
||
124 | ftpRequest.KeepAlive = true; |
||
125 | |||
126 | ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; |
||
127 | ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); |
||
128 | ftpStream = ftpResponse.GetResponseStream(); |
||
129 | FileStream localFileStream = new FileStream(localFile, FileMode.Create); |
||
130 | byte[] byteBuffer = new byte[bufferSize]; |
||
131 | int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize); |
||
132 | |||
133 | try |
||
134 | { |
||
135 | while (bytesRead > 0) |
||
136 | { |
||
137 | localFileStream.Write(byteBuffer, 0, bytesRead); |
||
138 | bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize); |
||
139 | } |
||
140 | } |
||
141 | catch (Exception ex) { Console.WriteLine(ex.ToString()); } |
||
142 | |||
143 | localFileStream.Close(); |
||
144 | ftpStream.Close(); |
||
145 | ftpResponse.Close(); |
||
146 | ftpRequest = null; |
||
147 | } |
||
148 | catch (Exception ex) |
||
149 | { |
||
150 | Console.WriteLine("err: " + ex.ToString()); |
||
151 | } |
||
152 | return; |
||
153 | } |
||
154 | |||
155 | static public void GetFileDate(string remoteFile, string localFile) |
||
156 | { |
||
157 | try |
||
158 | { |
||
159 | try |
||
160 | { |
||
161 | DateTime DateValue; |
||
162 | FileInfo LocalFileInfo = new FileInfo(System.Environment.CurrentDirectory + "/Program/" + remoteFile); |
||
163 | |||
164 | FtpWebRequest Request = (FtpWebRequest)WebRequest.Create(host + "/" + remoteFile); |
||
165 | Request.Credentials = new NetworkCredential(user, pass); |
||
166 | Request.Method = WebRequestMethods.Ftp.GetDateTimestamp; |
||
167 | Request.UseBinary = false; |
||
168 | |||
169 | using (FtpWebResponse Response = (FtpWebResponse)Request.GetResponse()) |
||
170 | using (TextReader Reader = new StringReader(Response.LastModified.ToString())) |
||
171 | { |
||
172 | string FTPDateString = Reader.ReadLine(); // FTP 서버의 파일 수정날짜 |
||
173 | string LocalDataString = LocalFileInfo.CreationTimeUtc.ToString(); |
||
174 | IniLogLoad(remoteFile, LocalDataString); |
||
175 | |||
176 | string FTPFileName = remoteFile.Substring(remoteFile.LastIndexOf('/') + 1, remoteFile.Length - remoteFile.LastIndexOf('/') - 1); |
||
177 | |||
178 | |||
179 | |||
180 | if (FTPDateString == LocalDataTime && FTPFileName == LocalDataName) |
||
181 | { |
||
182 | Console.WriteLine(remoteFile + " 받지 않음."); |
||
183 | } |
||
184 | else |
||
185 | { |
||
186 | download(remoteFile, System.Environment.CurrentDirectory + "/Program/" + remoteFile); |
||
187 | |||
188 | IniLogSave(remoteFile, null, FTPDateString); |
||
189 | |||
190 | MessageBox.Show(remoteFile + "다운받음"); |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | |||
195 | catch (Exception e) |
||
196 | { |
||
197 | Directory.CreateDirectory("Program/" + remoteFile); |
||
198 | |||
199 | FtpWebRequest Request = (FtpWebRequest)WebRequest.Create(host + remoteFile + "/"); |
||
200 | Request.Credentials = new NetworkCredential(user, pass); |
||
201 | Request.Method = WebRequestMethods.Ftp.ListDirectory; |
||
202 | Request.UseBinary = false; |
||
203 | StreamReader streamReader = new StreamReader(Request.GetResponse().GetResponseStream()); |
||
204 | List<string> list = new List<string>(); |
||
205 | |||
206 | |||
207 | while (true) |
||
208 | { |
||
209 | string fileName = streamReader.ReadLine(); |
||
210 | |||
211 | if (string.IsNullOrEmpty(fileName)) |
||
212 | { |
||
213 | break; |
||
214 | } |
||
215 | list.Add(fileName); |
||
216 | |||
217 | GetFileDate(remoteFile + "/"+fileName, System.Environment.CurrentDirectory + "/Program/"+ remoteFile+ "/" + fileName); |
||
218 | } |
||
219 | streamReader.Close(); |
||
220 | } |
||
221 | } |
||
222 | catch (Exception e) |
||
223 | { |
||
224 | MessageBox.Show(e.Message); |
||
225 | } |
||
226 | } |
||
227 | public List<string> GetFTPList(string targetURI, string userID, string password) |
||
228 | { |
||
229 | try |
||
230 | { |
||
231 | FtpWebRequest ftpWebRequest = WebRequest.Create(targetURI) as FtpWebRequest; |
||
232 | ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory; |
||
233 | ftpWebRequest.Credentials = new NetworkCredential(userID, password); |
||
234 | StreamReader streamReader = new StreamReader(ftpWebRequest.GetResponse().GetResponseStream()); |
||
235 | List<string> list = new List<string>(); |
||
236 | |||
237 | |||
238 | while (true) |
||
239 | { |
||
240 | string fileName = streamReader.ReadLine(); |
||
241 | |||
242 | if (string.IsNullOrEmpty(fileName)) |
||
243 | { |
||
244 | break; |
||
245 | } |
||
246 | list.Add(fileName); |
||
247 | |||
248 | //DirectoryInfo info = new DirectoryInfo(System.Environment.CurrentDirectory +"/Program/"+ fileName); |
||
249 | |||
250 | //if (!info.Exists) |
||
251 | //{ |
||
252 | // Directory.CreateDirectory(info.FullName); |
||
253 | //} |
||
254 | |||
255 | //foreach(var dir in info.FullName.ToString()) |
||
256 | //{ |
||
257 | // Console.WriteLine(dir); |
||
258 | //} |
||
259 | |||
260 | // Console.WriteLine(info.Exists); |
||
261 | |||
262 | // info.(System.Environment.CurrentDirectory + "/Program/" + fileName); |
||
263 | |||
264 | GetFileDate(fileName, System.Environment.CurrentDirectory + "/Program/" + fileName); |
||
265 | |||
266 | if (project == fileName) |
||
267 | { |
||
268 | } |
||
269 | else |
||
270 | { |
||
271 | } |
||
272 | } |
||
273 | streamReader.Close(); |
||
274 | return list; |
||
275 | } |
||
276 | catch(Exception e ) |
||
277 | { |
||
278 | MessageBox.Show(e.Message); |
||
279 | return null; |
||
280 | } |
||
281 | } |
||
282 | } |
||
283 | } |