905 lines
32 KiB
C#
905 lines
32 KiB
C#
//190806 chi getWorkWeek 추가
|
|
//190805 chi MakeCSVString 추가
|
|
//180903 chi makefilepath/ ftppath 추가
|
|
//180807 chi rad2deg, deg2rad 추가
|
|
//180625 chi ToCharHexString,ToStringFromHexString 추가
|
|
//180624 chi isLocalApplication 추가
|
|
//180618 chi GetCSVBuffer 추가
|
|
//180614 chi map 명령어 추가
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace FCOMMON
|
|
{
|
|
public static class AmkorRestfulService
|
|
{
|
|
|
|
public class RestfulResultDataTable
|
|
{
|
|
/// <summary>
|
|
/// 오류 및 기타상황의 메세지
|
|
/// </summary>
|
|
public string Message { get; set; }
|
|
|
|
/// <summary>
|
|
/// Restful 의 실행이 완료되었는가?
|
|
/// </summary>
|
|
public Boolean Complete { get; set; }
|
|
|
|
/// <summary>
|
|
/// Restful 실행 결과를 담고있는 RawBuffer
|
|
/// </summary>
|
|
public string Buffer { get; set; }
|
|
|
|
/// <summary>
|
|
/// Restful Service 호출 URL
|
|
/// </summary>
|
|
public string Url { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 결과값
|
|
/// </summary>
|
|
public System.Data.DataTable Result { get; set; }
|
|
public RestfulResultDataTable()
|
|
{
|
|
Complete = false;
|
|
Message = string.Empty;
|
|
Buffer = string.Empty;
|
|
Url = string.Empty;
|
|
Result = new System.Data.DataTable();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// SPM사이트에서 SID검색을 합니다.
|
|
/// </summary>
|
|
/// <param name="sid"></param>
|
|
/// <returns></returns>
|
|
public static RestfulResultDataTable SPMSIDSearch(string sid)
|
|
{
|
|
var retval = new RestfulResultDataTable();
|
|
retval.Url = "https://atknet.amkor.co.kr/spm/getActiveInventoryBySid/{0}";
|
|
retval.Url = string.Format(retval.Url, sid);
|
|
|
|
Boolean isError;
|
|
|
|
retval.Buffer = GetFromUrl(retval.Url, out isError);
|
|
|
|
var cols = new string[] { "plant", "location", "quantity", "part_description", "batch_no", "serial", "costcenter", "responsible", "bin_location" };
|
|
|
|
retval.Result = new DataTable();
|
|
foreach (var col in cols)
|
|
retval.Result.Columns.Add(col);
|
|
|
|
try
|
|
{
|
|
var json = JArray.Parse(retval.Buffer);
|
|
List<string> items = new List<string>();
|
|
string[] data = new string[cols.Length];
|
|
|
|
for (int i = 0; i < json.Count; i++)
|
|
{
|
|
var jdata = json[i];
|
|
for (int c = 0; c < cols.Length; c++)
|
|
{
|
|
var itemdata = jdata[cols[c]];
|
|
if (itemdata != null) data[c] = itemdata.ToString();
|
|
|
|
}
|
|
if (int.TryParse(data[2], out int value))
|
|
{
|
|
if (value > 0)
|
|
retval.Result.Rows.Add(data);
|
|
|
|
}
|
|
|
|
}
|
|
retval.Complete = retval.Result.Rows.Count > 0;
|
|
}
|
|
catch
|
|
{
|
|
retval.Complete = false;
|
|
}
|
|
|
|
retval.Result.AcceptChanges();
|
|
|
|
//if (retval.Buffer.ToLower().Contains("error") || isError)
|
|
//{
|
|
// retval.Complete = false;
|
|
// retval.Message = retval.Buffer;
|
|
// RaiseMessage(true, "get_tms_info Error : " + retval.Buffer);
|
|
//}
|
|
//else
|
|
//{
|
|
|
|
//string tmpBuffer = retval.Buffer;
|
|
|
|
//tmpBuffer = tmpBuffer.Replace((char)0x02, (char)0x0a);
|
|
//tmpBuffer = tmpBuffer.Replace((char)0x03, (char)0x09);
|
|
//retval.Result = GenerateDataTable(tmpBuffer);
|
|
//}
|
|
|
|
return retval;
|
|
}
|
|
public class MessageEventArgs : EventArgs
|
|
{
|
|
public Boolean isError { get; set; }
|
|
public string Message { get; set; }
|
|
public MessageEventArgs(bool iserr_, string msg_)
|
|
{
|
|
this.isError = iserr_;
|
|
this.Message = msg_;
|
|
}
|
|
}
|
|
public static event EventHandler<MessageEventArgs> Message;
|
|
|
|
/// <summary>
|
|
/// 웹서비스에서 받은 데이터 원문 메세지 입니다.
|
|
/// </summary>
|
|
public static event EventHandler<MessageEventArgs> RawMessage;
|
|
private static void RaiseMessage(Boolean iserr, string msg)
|
|
{
|
|
if (Message != null)
|
|
Message(null, new MessageEventArgs(iserr, msg));
|
|
}
|
|
private static void RaiseRawMessage(string url, string msg)
|
|
{
|
|
if (RawMessage != null)
|
|
RawMessage(null, new MessageEventArgs(false, "URL : " + url + "\n" + msg));
|
|
}
|
|
public static int timeout = 10000;
|
|
public static string LastQueryURL { get; private set; }
|
|
public static string LastQueryBUF { get; private set; }
|
|
|
|
private static string GetFromUrl(string url, out Boolean isError, string authid = "", string authpw = "")
|
|
{
|
|
isError = false;
|
|
string result = "";
|
|
try
|
|
{
|
|
RaiseMessage(false, "GET : " + url);
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
|
|
request.Timeout = timeout;
|
|
request.ReadWriteTimeout = timeout;
|
|
|
|
if (string.IsNullOrEmpty(authid) == false && string.IsNullOrEmpty(authpw) == false)
|
|
{
|
|
string authInfo = $"{authid}:{authpw}";
|
|
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
|
|
request.Headers["Authorization"] = "Basic " + authInfo;
|
|
}
|
|
|
|
request.MaximumAutomaticRedirections = 4;
|
|
request.MaximumResponseHeadersLength = 4;
|
|
request.Credentials = CredentialCache.DefaultCredentials;
|
|
var response = request.GetResponse() as HttpWebResponse;
|
|
var txtReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
|
|
result = txtReader.ReadToEnd();
|
|
|
|
LastQueryBUF = result;
|
|
LastQueryURL = url;
|
|
RaiseRawMessage(url, "RESULT\n" + result); //181026 - show data
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
isError = true;
|
|
result = ex.Message.ToString();
|
|
RaiseMessage(true, "GET-ERROR\n" + result);
|
|
LastQueryBUF = string.Empty;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
public static partial class Util
|
|
{
|
|
|
|
|
|
#region "flag"
|
|
|
|
|
|
//public static Boolean getBit(ref Int32 flag_, int idx)
|
|
//{
|
|
// return getBit(ref (UInt32)flag_, idx);
|
|
//}
|
|
public static Boolean getBit(Int32 flag_, int idx)
|
|
{
|
|
var offset = (UInt32)(1 << (int)idx);
|
|
return ((flag_ & offset) != 0);
|
|
}
|
|
public static void toggleBit(ref Int32 flag_, int idx)
|
|
{
|
|
var curValue = getBit(flag_, idx);
|
|
setBit(ref flag_, idx, !curValue);
|
|
}
|
|
// public static void setBit(ref Int32 flag_, int idx, Boolean value)
|
|
//{
|
|
// setBit(ref (UInt32)flag_, idx, value);
|
|
//}
|
|
public static void setBit(ref Int32 flag_, int idx, Boolean value)
|
|
{
|
|
UInt32 ovalue = (UInt32)flag_;
|
|
if (value)
|
|
{
|
|
var offset = (UInt32)(1 << (int)idx);
|
|
ovalue = ovalue | offset;
|
|
}
|
|
else
|
|
{
|
|
var offset = (UInt32)(~(1 << (int)idx));
|
|
ovalue = ovalue & offset;
|
|
}
|
|
flag_ = (Int32)ovalue;
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
public static bool savetoexcel(DataGridView dataGridView1,string fn)
|
|
{
|
|
libxl.Book book;// = new libxl.BinBook();
|
|
book = new libxl.XmlBook();
|
|
book.setKey(FCOMMON.info.libxlCompany, FCOMMON.info.libxlKey);
|
|
var sheet = book.addSheet("Data");
|
|
|
|
var row = 0;
|
|
var col = 0;
|
|
foreach (DataGridViewColumn column in dataGridView1.Columns)
|
|
{
|
|
sheet.writeStr(row, col++, column.HeaderText);
|
|
|
|
}
|
|
|
|
|
|
for (int i = 0; i < dataGridView1.RowCount; i++)
|
|
{
|
|
row += 1;
|
|
col = 0;
|
|
|
|
foreach (DataGridViewColumn column in dataGridView1.Columns)
|
|
{
|
|
var v = dataGridView1.Rows[i].Cells[column.Index].Value;
|
|
string value = "";
|
|
if (v != null) value = v.ToString();
|
|
|
|
if (column.ValueType == typeof(int))
|
|
{
|
|
var ivalue = (int)v;
|
|
sheet.writeNum(row, col++, ivalue);
|
|
}
|
|
else if (column.ValueType == typeof(decimal))
|
|
{
|
|
var ivalue = (decimal)v;
|
|
sheet.writeNum(row, col++, (double)ivalue);
|
|
}
|
|
else if (column.ValueType == typeof(float))
|
|
{
|
|
var ivalue = (float)v;
|
|
sheet.writeNum(row, col++, ivalue);
|
|
}
|
|
else if (column.ValueType == typeof(double))
|
|
{
|
|
var ivalue = (double)v;
|
|
sheet.writeNum(row, col++, ivalue);
|
|
}
|
|
else sheet.writeStr(row, col++, value);
|
|
}
|
|
}
|
|
book.save(fn);
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
public static string Number2Hangle(long lngNumber)
|
|
{
|
|
string[] NumberChar = new string[] { "", "일", "이", "삼"
|
|
, "사", "오", "육"
|
|
, "칠", "팔", "구" };
|
|
string[] LevelChar = new string[] { "", "십", "백", "천" };
|
|
string[] DecimalChar = new string[] { "", "만", "억", "조", "경" };
|
|
|
|
string strMinus = string.Empty;
|
|
|
|
if (lngNumber < 0)
|
|
{
|
|
strMinus = "마이너스";
|
|
lngNumber *= -1;
|
|
}
|
|
|
|
string strValue = string.Format("{0}", lngNumber);
|
|
string NumToKorea = string.Empty;
|
|
bool UseDecimal = false;
|
|
|
|
if (lngNumber == 0) return "영";
|
|
|
|
for (int i = 0; i < strValue.Length; i++)
|
|
{
|
|
int Level = strValue.Length - i;
|
|
if (strValue.Substring(i, 1) != "0")
|
|
{
|
|
UseDecimal = true;
|
|
if (((Level - 1) % 4) == 0)
|
|
{
|
|
if (DecimalChar[(Level - 1) / 4] != string.Empty
|
|
&& strValue.Substring(i, 1) == "1")
|
|
NumToKorea = NumToKorea + DecimalChar[(Level - 1) / 4];
|
|
else
|
|
NumToKorea = NumToKorea
|
|
+ NumberChar[int.Parse(strValue.Substring(i, 1))]
|
|
+ DecimalChar[(Level - 1) / 4];
|
|
UseDecimal = false;
|
|
}
|
|
else
|
|
{
|
|
if (strValue.Substring(i, 1) == "1")
|
|
NumToKorea = NumToKorea
|
|
+ LevelChar[(Level - 1) % 4];
|
|
else
|
|
NumToKorea = NumToKorea
|
|
+ NumberChar[int.Parse(strValue.Substring(i, 1))]
|
|
+ LevelChar[(Level - 1) % 4];
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if ((Level % 4 == 0) && UseDecimal)
|
|
{
|
|
NumToKorea = NumToKorea + DecimalChar[Level / 4];
|
|
UseDecimal = false;
|
|
}
|
|
}
|
|
}
|
|
return strMinus + NumToKorea;
|
|
}
|
|
|
|
|
|
public static bool IsNumeric(string input)
|
|
{
|
|
double data;
|
|
return double.TryParse(input, out data);
|
|
//return Regex.IsMatch(input, @"^\d+$");
|
|
}
|
|
|
|
public static string MakeFilterString(string[] cols, string search)
|
|
{
|
|
|
|
|
|
string filterStr = "";
|
|
foreach (var col in cols)
|
|
{
|
|
if (filterStr != "") filterStr += " OR ";
|
|
filterStr += string.Format("isnull({0},'') like '%#%'", col);
|
|
}
|
|
return filterStr.Replace("#", search.Replace("'", "''"));
|
|
}
|
|
public static void CopyData(System.Data.DataRow drFrom, System.Data.DataRow drTo)
|
|
{
|
|
for (int i = 0; i < drFrom.ItemArray.Length; i++)
|
|
drTo[i] = drFrom[i];
|
|
drTo.EndEdit();
|
|
}
|
|
public static int GetWorkWeek()
|
|
{
|
|
return GetWorkWeek(DateTime.Now);
|
|
}
|
|
public static int GetWorkWeek(DateTime date)
|
|
{
|
|
var dfi = System.Globalization.DateTimeFormatInfo.CurrentInfo;
|
|
var date1 = date;
|
|
var cal = dfi.Calendar;
|
|
var week = cal.GetWeekOfYear(date1, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
|
|
return week;
|
|
}
|
|
|
|
public static string MakeFilePath(params string[] param)
|
|
{
|
|
System.Text.StringBuilder sb = new System.Text.StringBuilder();
|
|
foreach (var item in param)
|
|
{
|
|
if (sb.Length > 0 && sb.ToString().EndsWith("\\") == false) sb.Append("\\");
|
|
sb.Append(item.Replace("/", "\\"));
|
|
}
|
|
var retval = sb.ToString().Replace("/", "\\").Replace("\\\\", "\\");
|
|
return retval.ToString();
|
|
}
|
|
|
|
public static string MakeFTPPath(params string[] param)
|
|
{
|
|
System.Text.StringBuilder sb = new System.Text.StringBuilder();
|
|
foreach (var item in param)
|
|
{
|
|
if (sb.Length > 0 && sb.ToString().EndsWith("/") == false) sb.Append("/");
|
|
sb.Append(item.Replace("\\", "/"));
|
|
}
|
|
var retval = sb.ToString().Replace("//", "/");
|
|
return retval.ToString();
|
|
}
|
|
|
|
|
|
public static double rad2Deg(double source)
|
|
{
|
|
return (source * 1.0) * 180.0 / Math.PI;
|
|
}
|
|
public static double deg2rad(double source)
|
|
{
|
|
return (source * 1.0) * Math.PI / 180.0;
|
|
}
|
|
/// <summary>
|
|
/// 문자를 HEX 문자로 변경합니다. 각 HEX문자 사이에는 공백이 없습니다.
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public static string ToCharHexString(string input)
|
|
{
|
|
System.Text.StringBuilder sb = new System.Text.StringBuilder();
|
|
foreach (char b in input.ToCharArray())
|
|
sb.Append(((byte)b).ToString("X2"));
|
|
return sb.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 16진수형태의 문자를 일반 문자열로 반환한니다.
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public static string ToStringFromHexString(string input)
|
|
{
|
|
int wordCount = (int)(input.Length / 2);
|
|
int n = (int)(input.Length % 2);
|
|
if (n != 0) return "X:Length Error";
|
|
|
|
System.Text.StringBuilder sb = new StringBuilder();
|
|
for (int i = 0; i < wordCount; i++)
|
|
{
|
|
string hexstr = input.Substring(i * 2, 2);
|
|
try
|
|
{
|
|
byte ascvalue = Convert.ToByte(hexstr, 16);
|
|
sb.Append((char)ascvalue);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ex.Message;
|
|
}
|
|
}
|
|
//두자리씩 끊어서 16진수로 변환하고 ASC값을 누적한다.
|
|
return sb.ToString();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 현재 프로그램이 개인용위치에서 실행중인가(클릭원스의경우)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static Boolean isLocalApplication()
|
|
{
|
|
var localpath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
|
return Util.CurrentPath.StartsWith(localpath);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 문자열 배열을 CSV라인으로 변환
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
public static string MakeCSVString(params string[] param)
|
|
{
|
|
System.Text.StringBuilder sb = new StringBuilder();
|
|
foreach (var data in param)
|
|
{
|
|
if (sb.Length > 0) sb.Append(',');
|
|
sb.Append(ToCSVString(data));
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// CSV데이터포맷으로 버퍼를 반환합니다. 문자열내에 , 가 있는 데이터는 쌍따옴표로 구분합니다.
|
|
/// </summary>
|
|
/// <param name="line"></param>
|
|
/// <returns></returns>
|
|
public static string[] GetCSVBuffer(string line)
|
|
{
|
|
List<string> buffer = new List<string>();
|
|
if (line.Trim() == "") return buffer.ToArray();
|
|
|
|
System.Text.StringBuilder sb = new StringBuilder();
|
|
bool findsig = false;
|
|
var charbuf = line.ToCharArray();
|
|
char pchar = '\0';
|
|
bool findCommaString = false;
|
|
foreach (var c in charbuf)
|
|
{
|
|
if (c == ',')
|
|
{
|
|
if (findsig) sb.Append(c); //대상에 콤마가 잇으므로 게소 누적한다.
|
|
else if (findCommaString == false)
|
|
{
|
|
//데이터를 분리해줘야함
|
|
buffer.Add(sb.ToString());
|
|
sb.Clear();
|
|
findsig = false;
|
|
}
|
|
else findCommaString = false;
|
|
}
|
|
else if (c == '\"')
|
|
{
|
|
if (pchar == ',')
|
|
{
|
|
if (!findsig) findsig = true;
|
|
else findsig = false; //완료된 경우이다.
|
|
}
|
|
else if (!findsig)
|
|
{
|
|
sb.Append(c);
|
|
}
|
|
else
|
|
{
|
|
buffer.Add(sb.ToString());
|
|
sb.Clear();
|
|
findsig = false;
|
|
findCommaString = true;
|
|
}
|
|
//if (!findsig) findsig = true;
|
|
//else sb.Append(c);
|
|
}
|
|
else
|
|
{
|
|
sb.Append(c);
|
|
}
|
|
pchar = c;
|
|
}
|
|
//if(sb.Length > 0)
|
|
//{
|
|
buffer.Add(sb.ToString());
|
|
//}
|
|
return buffer.ToArray();
|
|
}
|
|
|
|
public static double map(double source, int in_min, int in_max, int out_min, int out_max)
|
|
{
|
|
return (source - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
|
}
|
|
|
|
|
|
#region "MessageBox"
|
|
public static void MsgI(string m, params string[] args)
|
|
{
|
|
m = string.Format(m, args);
|
|
MessageBox.Show(m, "확인", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
public static void MsgE(string m, params string[] args)
|
|
{
|
|
m = string.Format(m, args);
|
|
MessageBox.Show(m, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
public static DialogResult MsgQ(string m, params string[] args)
|
|
{
|
|
m = string.Format(m, args);
|
|
DialogResult dlg = MessageBox.Show(m, "확인", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
|
|
return dlg;
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
public static void SaveBugReport(string content, string subdirName = "BugReport")
|
|
{
|
|
try
|
|
{
|
|
var path = CurrentPath + subdirName;
|
|
if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path);
|
|
var file = path + "\\" + DateTime.Now.ToString("yyyyMMdd_HHmmss_fff") + ".txt";
|
|
System.IO.File.WriteAllText(file, content, System.Text.Encoding.UTF8);
|
|
}
|
|
catch
|
|
{
|
|
//nothing
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 현재실행중인폴더를 반환합니다.
|
|
/// </summary>
|
|
public static string CurrentPath
|
|
{
|
|
get
|
|
{
|
|
return AppDomain.CurrentDomain.BaseDirectory;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 콤마와 줄바꿈등을 제거합니다.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string ToCSVString(string src)
|
|
{
|
|
if (src == null) return string.Empty;
|
|
string strdata = strdata = src.Replace("\r", "").Replace("\n", "");
|
|
if (strdata.IndexOf(',') != -1)
|
|
{
|
|
strdata = "\"" + strdata + "\""; //180618 콤마가들어가는 csv 처리
|
|
}
|
|
return strdata;
|
|
}
|
|
|
|
public static Boolean RunProcess(string file, string arg = "")
|
|
{
|
|
var fi = new System.IO.FileInfo(file);
|
|
if (!fi.Exists)
|
|
{
|
|
return false;
|
|
}
|
|
System.Diagnostics.Process prc = new System.Diagnostics.Process();
|
|
System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo(file);
|
|
si.Arguments = arg;
|
|
prc.StartInfo = si;
|
|
prc.Start();
|
|
return true;
|
|
}
|
|
|
|
#region "NIC"
|
|
|
|
/// <summary>
|
|
/// 지정된 nic카드가 현재 목록에 존재하는지 확인한다.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static Boolean ExistNIC(string NICName)
|
|
{
|
|
if (string.IsNullOrEmpty(NICName)) return false;
|
|
foreach (string NetName in NICCardList())
|
|
{
|
|
if (NetName.ToLower() == NICName.ToLower())
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ehternet Card 를 사용안함으로 설정합니다.(관리자권한필요)
|
|
/// </summary>
|
|
/// <param name="NicName"></param>
|
|
public static Boolean NICDisable(string NICName)
|
|
{
|
|
//해당 nic 가 현재 목록에 존재하는지 확인한다.
|
|
|
|
string cmd = "interface set interface " + NICName + " disable";
|
|
Process prc = new Process();
|
|
ProcessStartInfo si = new ProcessStartInfo("netsh", cmd);
|
|
si.WindowStyle = ProcessWindowStyle.Hidden;
|
|
prc.StartInfo = si;
|
|
prc.Start();
|
|
|
|
////목록에서 사라질때까지 기다린다.
|
|
DateTime SD = DateTime.Now;
|
|
Boolean timeout = false;
|
|
while ((true))
|
|
{
|
|
|
|
bool FindNetwork = false;
|
|
foreach (string NetName in NICCardList())
|
|
{
|
|
if (NetName == NICName.ToLower())
|
|
{
|
|
FindNetwork = true;
|
|
break; // TODO: might not be correct. Was : Exit For
|
|
}
|
|
}
|
|
|
|
if (!FindNetwork)
|
|
break; // TODO: might not be correct. Was : Exit While
|
|
|
|
System.Threading.Thread.Sleep(1000);
|
|
TimeSpan ts = DateTime.Now - SD;
|
|
if (ts.TotalSeconds > 10)
|
|
{
|
|
timeout = true;
|
|
break; // TODO: might not be correct. Was : Exit While
|
|
}
|
|
}
|
|
return !timeout;
|
|
}
|
|
|
|
public static List<String> NICCardList()
|
|
{
|
|
List<String> Retval = new List<string>();
|
|
foreach (System.Net.NetworkInformation.NetworkInterface Net in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
|
|
{
|
|
if (Net.NetworkInterfaceType == System.Net.NetworkInformation.NetworkInterfaceType.Ethernet)
|
|
{
|
|
Retval.Add(Net.Name.ToUpper());
|
|
}
|
|
}
|
|
return Retval;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 이더넷카드를 사용함으로 설정합니다.
|
|
/// </summary>
|
|
/// <param name="NicName"></param>
|
|
public static Boolean NICEnable(string NICName)
|
|
{
|
|
string cmd = "interface set interface " + NICName + " enable";
|
|
System.Diagnostics.Process prc = new System.Diagnostics.Process();
|
|
System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo("netsh", cmd);
|
|
si.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
|
|
prc.StartInfo = si;
|
|
prc.Start();
|
|
|
|
|
|
////목록에생길떄까지 대기
|
|
DateTime SD = DateTime.Now;
|
|
while ((true))
|
|
{
|
|
|
|
bool FindNetwork = false;
|
|
foreach (string NetName in NICCardList())
|
|
{
|
|
if (NetName.ToLower() == NICName.ToLower())
|
|
{
|
|
FindNetwork = true;
|
|
break; // TODO: might not be correct. Was : Exit For
|
|
}
|
|
}
|
|
|
|
if (FindNetwork)
|
|
break; // TODO: might not be correct. Was : Exit While
|
|
|
|
System.Threading.Thread.Sleep(1000);
|
|
TimeSpan ts = DateTime.Now - SD;
|
|
if (ts.TotalSeconds > 10)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
////결이 완료될떄까지 기다린다.
|
|
SD = DateTime.Now;
|
|
while ((true))
|
|
{
|
|
|
|
bool FindNetwork = false;
|
|
foreach (System.Net.NetworkInformation.NetworkInterface Net in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
|
|
{
|
|
if (Net.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.GigabitEthernet &&
|
|
Net.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Ethernet) continue;
|
|
if (Net.Name.ToLower() == NICName.ToLower())
|
|
{
|
|
//string data = Net.GetIPProperties().GatewayAddresses[0].ToString();
|
|
|
|
if (Net.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up)
|
|
{
|
|
|
|
FindNetwork = true;
|
|
break; // TODO: might not be correct. Was : Exit For
|
|
}
|
|
}
|
|
}
|
|
if (FindNetwork)
|
|
return true;
|
|
|
|
System.Threading.Thread.Sleep(1000);
|
|
TimeSpan ts = DateTime.Now - SD;
|
|
if (ts.TotalSeconds > 10)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
public static void RunExplorer(string arg)
|
|
{
|
|
System.Diagnostics.ProcessStartInfo si = new ProcessStartInfo("explorer");
|
|
si.Arguments = arg;
|
|
System.Diagnostics.Process.Start(si);
|
|
}
|
|
public static void RunDefaultMail(string to, string title, string content = "", string cc = "", string bcc = "")
|
|
{
|
|
string args = "mailto:" + to + "?";
|
|
if (title != "") args += "subject=" + title;
|
|
args += "&IsBodyHtml=true";
|
|
if (content != "")
|
|
{
|
|
if (!args.EndsWith("?")) args += "&";
|
|
args += "HtmlBody=" + content;
|
|
}
|
|
if (bcc != "")
|
|
{
|
|
if (!args.EndsWith("?")) args += "&";
|
|
args += "bcc=" + bcc;
|
|
}
|
|
if (cc != "")
|
|
{
|
|
if (!args.EndsWith("?")) args += "&";
|
|
args += "cc=" + cc;
|
|
}
|
|
if (bcc != "")
|
|
{
|
|
if (!args.EndsWith("?")) args += "&";
|
|
args += "bcc=" + bcc;
|
|
}
|
|
|
|
|
|
|
|
|
|
System.Diagnostics.Process.Start(args);
|
|
}
|
|
|
|
#region "watchdog"
|
|
public static void WatchDog_Run()
|
|
{
|
|
System.IO.FileInfo fi = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "WatchCat.exe");
|
|
if (!fi.Exists) return;
|
|
var Exist = CheckExistProcess("watchcat");
|
|
if (Exist) return;
|
|
RunProcess(fi.FullName);
|
|
}
|
|
|
|
public static Boolean CheckExistProcess(string ProcessName)
|
|
{
|
|
foreach (var prc in System.Diagnostics.Process.GetProcesses())
|
|
{
|
|
if (prc.ProcessName.StartsWith("svchost")) continue;
|
|
if (prc.ProcessName.ToUpper() == ProcessName.ToUpper()) return true;
|
|
}
|
|
return false;
|
|
}
|
|
#endregion
|
|
|
|
#region "web function"
|
|
/// <summary>
|
|
/// URL로부터 문자열을 수신합니다.
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="isError"></param>
|
|
/// <returns></returns>
|
|
public static string GetStrfromurl(string url, out Boolean isError)
|
|
{
|
|
isError = false;
|
|
string result = "";
|
|
try
|
|
{
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
|
|
request.Timeout = 60000;
|
|
request.ReadWriteTimeout = 60000;
|
|
|
|
request.MaximumAutomaticRedirections = 4;
|
|
request.MaximumResponseHeadersLength = 4;
|
|
request.Credentials = CredentialCache.DefaultCredentials;
|
|
var response = request.GetResponse() as HttpWebResponse;
|
|
var txtReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
|
|
result = txtReader.ReadToEnd();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
isError = true;
|
|
result = ex.Message.ToString();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|