Esempio n. 1
0
        public static Object ObjectToXML(string xml, Type objectType)
        {
            StringReader  strReader  = null;
            XmlSerializer serializer = null;
            XmlTextReader xmlReader  = null;
            Object        obj        = null;

            try
            {
                strReader  = new StringReader(xml);
                serializer = new XmlSerializer(objectType);
                xmlReader  = new XmlTextReader(strReader);
                obj        = serializer.Deserialize(xmlReader);
            }
            catch (Exception exp)
            {
                SharpLog.Error("ObjectToXML", exp.ToString(), "GetXMLFromObject");
            }
            finally
            {
                if (xmlReader != null)
                {
                    xmlReader.Close();
                }
                if (strReader != null)
                {
                    strReader.Close();
                }
            }
            return(obj);
        }
Esempio n. 2
0
        public string Read(string KeyName)
        {
            // Open a subKey as read-only
            RegistryKey sk1 = baseRegistryKey.OpenSubKey(subKey);

            // If the RegistrySubKey doesn't exist -> (null)
            if (sk1 == null)
            {
                return(null);
            }
            else
            {
                try
                {
                    // If the RegistryKey exists I get its value
                    // or null is returned.
                    return(sk1.GetValue(KeyName.ToUpper()).ToString());
                }
                catch (Exception e)
                {
                    SharpLog.Error("Reading registry ", KeyName.ToUpper() + ":" + e.ToString());
                    return(null);
                }
            }
        }
Esempio n. 3
0
        public object OpenWebPage(ref object url, bool isShow = true)
        {
            try
            {
                if (isShow)
                {
                    ie.Visible = true;
                }
                else
                {
                    ie.Visible = false;
                }

                ie.Navigate2(ref url);

                while (true)
                {
                    if (!isBusy())
                    {
                        break;
                    }

                    Thread.Sleep(1000);
                }

                return(ie.Document);
            }
            catch (Exception ex)
            {
                SharpLog.Error("OpenWebPage", ex.ToString());
            }

            return(null);
        }
Esempio n. 4
0
        public static string GetXMLFromObject(object o)
        {
            StringWriter  sw = new StringWriter();
            XmlTextWriter tw = null;

            try
            {
                XmlSerializer serializer = new XmlSerializer(o.GetType());
                tw = new XmlTextWriter(sw);
                serializer.Serialize(tw, o);
            }
            catch (Exception ex)
            {
                sw = null;
                SharpLog.Error("GetXMLFromObject", ex.ToString(), "GetXMLFromObject");
            }
            finally
            {
                sw.Close();
                if (tw != null)
                {
                    tw.Close();
                }
            }
            return(sw.ToString());
        }
Esempio n. 5
0
        private static bool inject(string dllPath, Process tProcess)
        {
            try
            {
                Process targetProcess = tProcess;
                string  dllName       = dllPath;

                // the target process
                // geting the handle of the process - with required privileges
                IntPtr procHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, targetProcess.Id);
                // name of the dll we want to inject
                // alocating some memory on the target process - enough to store the name of the dll
                // and storing its address in a pointer
                IntPtr allocMemAddress = VirtualAllocEx(procHandle, IntPtr.Zero, (uint)((dllName.Length + 1) * Marshal.SizeOf(typeof(char))), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
                // writing the name of the dll there
                UIntPtr bytesWritten;
                WriteProcessMemory(procHandle, allocMemAddress, Encoding.Default.GetBytes(dllName), (uint)((dllName.Length + 1) * Marshal.SizeOf(typeof(char))), out bytesWritten);
                // searching for the address of LoadLibraryA and storing it in a pointer
                IntPtr loadLibraryAddr = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
                // creating a thread that will call LoadLibraryA with allocMemAddress as argument
                CreateRemoteThread(procHandle, IntPtr.Zero, 0, loadLibraryAddr, allocMemAddress, 0, IntPtr.Zero);
            }
            catch (Exception ex)
            {
                SharpLog.Error("inject", ex.ToString());
                return(false);
            }

            return(true);
        }
Esempio n. 6
0
 public void Initialize()
 {
     try
     {
         WindowProc.KillProcess("iexplore");
         ie = new SHDocVw.InternetExplorer();
     }
     catch (Exception ex)
     {
         SharpLog.Error("WebProcInit", ex.ToString());
     }
 }
Esempio n. 7
0
        public void GetAsyncRequest(string pUrl, string host, string referhost, string cookies, object param, bool islog = false)
        {
            if (islog)
            {
                SharpLog.Debug("GetAsyncRequest", pUrl);
            }

            if (pUrl != "" && pUrl != "-1")
            {
                wRequest.GetAsyncTask(pUrl, host, referhost, requestGetCallBack, cookies, param);
            }
        }
Esempio n. 8
0
        public bool UpdateExcel(DataTable data, string sheetName = null)
        {
            ISheet    sheet    = null;
            IWorkbook workbook = null;

            using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                workbook = new XSSFWorkbook(fs);
            }
            if (workbook == null)
            {
                return(false);
            }

            using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                try
                {
                    if (!sheetName.IsEmptyOrWhiteSpace())
                    {
                        sheet = workbook.GetSheet(sheetName);
                    }
                    else
                    {
                        sheet = workbook.GetSheetAt(0);
                    }

                    int rowCount = sheet.LastRowNum;
                    for (int i = 0; i < data.Rows.Count; ++i)
                    {
                        int idIndex = data.Rows[i][0].ConvertTo <Int32>();

                        IRow  row   = sheet.GetRow(idIndex + 1);
                        ICell icell = row.CreateCell(2);
                        icell.SetCellValue(data.Rows[i][1].ToString());
                    }
                    workbook.Write(fs); //写入到excel
                }
                catch (Exception ex)
                {
                    SharpLog.Error("InsertDataTableToExcel", ex.ToString());
                    return(false);
                }
                finally
                {
                    workbook.Close();
                }
            }

            return(true);
        }
Esempio n. 9
0
 public void CloseDB()
 {
     try
     {
         if (conn.State != ConnectionState.Closed)
         {
             conn.Close();
         }
     }
     catch (Exception ex)
     {
         SharpLog.Error("CloseDB", ex.ToString());
         throw new Exception(ex.ToString());
     }
 }
Esempio n. 10
0
 public void Close()
 {
     try
     {
         if (ie != null)
         {
             //ie.Quit();
             WindowProc.KillProcess("iexplore");
         }
     }
     catch (Exception ex)
     {
         SharpLog.Error("WebProcClose", ex.ToString());
     }
 }
Esempio n. 11
0
 public MySqlDataReader MySqlDataRead(string sql)
 {
     try
     {
         OpenDB();
         MySqlCommand mySqlCommand1 = new MySqlCommand(sql, conn);
         mySqlCommand1.Prepare();
         MySqlDataReader mySqlDataReader1 = mySqlCommand1.ExecuteReader();
         return(mySqlDataReader1);
     }
     catch (Exception ex)
     {
         SharpLog.Error("MySqlDataRead", ex.ToString(), sql);
         return(null);
     }
 }
Esempio n. 12
0
 /// <summary>
 /// 프로세스 종료
 /// </summary>
 /// <param name="exeName"></param>
 /// <returns></returns>
 public static bool KillProcess(string exeName)
 {
     try
     {
         foreach (var process in Process.GetProcessesByName(exeName))
         {
             process.Kill();
         }
     }
     catch (Exception ex)
     {
         SharpLog.Error("killProcess", ex.ToString());
         return(false);
     }
     return(true);
 }
Esempio n. 13
0
        public bool MySqlQueryExecute(string strQuery)
        {
            try
            {
                OpenDB();
                MySqlCommand mySqlCommand = new MySqlCommand(strQuery, conn);
                mySqlCommand.Prepare();
                mySqlCommand.ExecuteNonQuery();

                return(true);
            }
            catch (Exception ex)
            {
                SharpLog.Error("MySqlQueryExecute", ex.ToString(), strQuery);
                return(false);
            }
        }
Esempio n. 14
0
        public DataSet MySqlQueryRead(string strQuery)
        {
            try
            {
                DataSet dataSet = new DataSet();

                OpenDB();
                MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(strQuery, conn);
                mySqlDataAdapter.Fill(dataSet);
                return(dataSet);
            }
            catch (Exception ex)
            {
                SharpLog.Error("MySqlQueryRead", ex.ToString(), strQuery);
                return(null);
            }
        }
Esempio n. 15
0
        public bool Write(string KeyName, object Value)
        {
            try
            {
                // 'cause OpenSubKey open a subKey as read-only
                RegistryKey sk1 = baseRegistryKey.CreateSubKey(subKey);
                // Save the value
                sk1.SetValue(KeyName.ToUpper(), Value);

                return(true);
            }
            catch (Exception e)
            {
                SharpLog.Error("Writing registry ", KeyName.ToUpper() + ":" + e.ToString());
                return(false);
            }
        }
Esempio n. 16
0
        public bool DeleteSubKeyTree()
        {
            try
            {
                RegistryKey sk1 = baseRegistryKey.OpenSubKey(subKey);
                // If the RegistryKey exists, I delete it
                if (sk1 != null)
                {
                    baseRegistryKey.DeleteSubKeyTree(subKey);
                }

                return(true);
            }
            catch (Exception e)
            {
                SharpLog.Error("Deleting SubKey ", e.ToString());
                return(false);
            }
        }
Esempio n. 17
0
 public int ValueCount()
 {
     try
     {
         RegistryKey sk1 = baseRegistryKey.OpenSubKey(subKey);
         // If the RegistryKey exists...
         if (sk1 != null)
         {
             return(sk1.ValueCount);
         }
         else
         {
             return(0);
         }
     }
     catch (Exception e)
     {
         SharpLog.Error("Retriving keys ", e.ToString());
         return(0);
     }
 }
Esempio n. 18
0
        public bool DeleteKey(string KeyName)
        {
            try
            {
                RegistryKey sk1 = baseRegistryKey.CreateSubKey(subKey);
                // If the RegistrySubKey doesn't exists -> (true)
                if (sk1 == null)
                {
                    return(true);
                }
                else
                {
                    sk1.DeleteValue(KeyName);
                }

                return(true);
            }
            catch (Exception e)
            {
                SharpLog.Error("Deleting mainKey ", KeyName.ToUpper() + ":" + e.ToString());
                return(false);
            }
        }
Esempio n. 19
0
        public int CreateDataTableToExcel(DataTable data, string sheetName, bool isColumnWritten)
        {
            int    i     = 0;
            int    j     = 0;
            int    count = 0;
            ISheet sheet = null;

            IWorkbook workbook = null;

            using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                if (fileName.IndexOf(".xlsx") > 0) // 2007版本
                {
                    workbook = new XSSFWorkbook();
                }
                else if (fileName.IndexOf(".xls") > 0) // 2003版本
                {
                    workbook = new HSSFWorkbook();
                }

                try
                {
                    if (workbook != null)
                    {
                        sheet = workbook.CreateSheet(sheetName);
                    }
                    else
                    {
                        return(-1);
                    }

                    if (isColumnWritten == true) //写入DataTable的列名
                    {
                        IRow row = sheet.CreateRow(0);
                        for (j = 0; j < data.Columns.Count; ++j)
                        {
                            row.CreateCell(j).SetCellValue(data.Columns[j].ColumnName);
                        }
                        count = 1;
                    }
                    else
                    {
                        count = 0;
                    }

                    for (i = 0; i < data.Rows.Count; ++i)
                    {
                        IRow row = sheet.CreateRow(count);
                        for (j = 0; j < data.Columns.Count; ++j)
                        {
                            row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString());
                        }
                        ++count;
                    }
                    workbook.Write(fs); //写入到excel
                }
                catch (Exception ex)
                {
                    SharpLog.Error("DataTableToExcel", ex.ToString());
                    return(-1);
                }
            }

            workbook.Close();

            return(count);
        }
Esempio n. 20
0
        /// 将excel中的数据导入到DataTable中
        /// </summary>
        /// <param name="sheetName">excel工作薄sheet的名称</param>
        /// <param name="isFirstRowColumn">第一行是否是DataTable的列名</param>
        /// <returns>返回的DataTable</returns>
        public DataTable ExcelToDataTable(string sheetName, bool isFirstRowColumn = false)
        {
            ISheet    sheet    = null;
            DataTable data     = new DataTable();
            int       startRow = 0;
            IWorkbook workbook = null;

            try
            {
                FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                if (fileName.IndexOf(".xlsx") > 0) // 2007版本
                {
                    workbook = new XSSFWorkbook(fs);
                }
                else if (fileName.IndexOf(".xls") > 0) // 2003版本
                {
                    workbook = new HSSFWorkbook(fs);
                }

                if (sheetName != null)
                {
                    sheet = workbook.GetSheet(sheetName);
                    if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
                    {
                        sheet = workbook.GetSheetAt(0);
                    }
                }
                else
                {
                    sheet = workbook.GetSheetAt(0);
                }
                if (sheet != null)
                {
                    IRow firstRow  = sheet.GetRow(0);
                    int  cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数

                    if (isFirstRowColumn)
                    {
                        for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                        {
                            ICell cell = firstRow.GetCell(i);
                            if (cell != null)
                            {
                                string cellValue = cell.StringCellValue;
                                if (cellValue != null)
                                {
                                    DataColumn column = new DataColumn(cellValue);
                                    data.Columns.Add(column);
                                }
                            }
                        }
                        startRow = sheet.FirstRowNum + 1;
                    }
                    else
                    {
                        startRow = sheet.FirstRowNum;
                    }

                    //最后一列的标号
                    int rowCount = sheet.LastRowNum;
                    for (int i = startRow; i <= rowCount; ++i)
                    {
                        IRow row = sheet.GetRow(i);
                        if (row == null)
                        {
                            continue;              //没有数据的行默认是null       
                        }
                        DataRow dataRow = data.NewRow();
                        for (int j = row.FirstCellNum; j < cellCount; ++j)
                        {
                            if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null
                            {
                                dataRow[j] = row.GetCell(j).ToString();
                            }
                        }
                        data.Rows.Add(dataRow);
                    }
                }

                workbook.Close();

                return(data);
            }
            catch (Exception ex)
            {
                SharpLog.Error("ExcelToDataTable", ex.ToString());
                return(null);
            }
        }
Esempio n. 21
0
        public int InsertDataTableToExcel(DataTable data, string sheetName, bool isColumnWritten)
        {
            int    i     = 0;
            int    j     = 0;
            int    count = 0;
            ISheet sheet = null;

            IWorkbook workbook = null;

            using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                workbook = new XSSFWorkbook(fs);

                //for (int i = 0; i < wb.Count; i++)
                //{
                //    comboBox1.Items.Add(wb.GetSheetAt(i).SheetName);
                //}
            }


            using (FileStream fs = new FileStream(fileName, FileMode.Append, FileAccess.Write))
            {
                //if (fileName.IndexOf(".xlsx") > 0) // 2007版本
                //    workbook = new XSSFWorkbook();
                //else if (fileName.IndexOf(".xls") > 0) // 2003版本
                //    workbook = new HSSFWorkbook();

                try
                {
                    if (workbook != null)
                    {
                        sheet = workbook.GetSheet(sheetName);
                        if (sheet == null)
                        {
                            sheet = workbook.CreateSheet(sheetName);
                        }
                    }
                    else
                    {
                        return(-1);
                    }

                    if (isColumnWritten == true) //写入DataTable的列名
                    {
                        IRow row = sheet.CreateRow(0);
                        for (j = 0; j < data.Columns.Count; ++j)
                        {
                            row.CreateCell(j).SetCellValue(data.Columns[j].ColumnName);
                        }
                        count = 1 + sheet.LastRowNum;
                    }
                    else
                    {
                        count = sheet.LastRowNum;
                    }

                    for (i = 0; i < data.Rows.Count; ++i)
                    {
                        IRow row = sheet.CreateRow(count);
                        for (j = 0; j < data.Columns.Count; ++j)
                        {
                            row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString());
                        }
                        ++count;
                    }
                    workbook.Write(fs); //写入到excel
                }
                catch (Exception ex)
                {
                    SharpLog.Error("InsertDataTableToExcel", ex.ToString());
                    return(-1);
                }
            }

            workbook.Close();
            return(count);
        }