วันอังคารที่ 29 พฤศจิกายน พ.ศ. 2554

C#: Set And Unset Auto-start For Windows Application


I found this useful C# class in this MSDN forum that provides an easy way to enable and disable an application auto-run in Windows.

With a little modification, I made it to be easily called by any project. What you need to do is just pass in the 'Key Name' and 'Assembly Location' parameters.

Here is the complete class:

วันศุกร์ที่ 25 พฤศจิกายน พ.ศ. 2554

EX Ping c#

 
/*
C# Network Programming 
by Richard Blum

Publisher: Sybex 
ISBN: 0782141765
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SimplePing
{
   public static void Main(string[] argv)
   {
      byte[] data = new byte[1024];
      int recv;
      Socket host = new Socket(AddressFamily.InterNetwork, SocketType.Raw,
                 ProtocolType.Icmp);
      IPEndPoint iep = new IPEndPoint(IPAddress.Parse(argv[0])0);
      EndPoint ep = (EndPoint)iep;
      ICMP packet = new ICMP();

      packet.Type = 0x08;
      packet.Code = 0x00;
      packet.Checksum = 0;
      Buffer.BlockCopy(BitConverter.GetBytes((short)1)0, packet.Message, 02);
      Buffer.BlockCopy(BitConverter.GetBytes((short)1)0, packet.Message, 22);
      data = Encoding.ASCII.GetBytes("test packet");
      Buffer.BlockCopy(data, 0, packet.Message, 4, data.Length);
      packet.MessageSize = data.Length + 4;
      int packetsize = packet.MessageSize + 4;

      UInt16 chcksum = packet.getChecksum();
      packet.Checksum = chcksum;

      host.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);
      host.SendTo(packet.getBytes(), packetsize, SocketFlags.None, iep);
      try
      {
         data = new byte[1024];
         recv = host.ReceiveFrom(data, ref ep);
      catch (SocketException)
      {
         Console.WriteLine("No response from remote host");
         return;
      }
      ICMP response = new ICMP(data, recv);
      Console.WriteLine("response from: {0}", ep.ToString());
      Console.WriteLine("  Type {0}", response.Type);
      Console.WriteLine("  Code: {0}", response.Code);
      int Identifier = BitConverter.ToInt16(response.Message, 0);
      int Sequence = BitConverter.ToInt16(response.Message, 2);
      Console.WriteLine("  Identifier: {0}", Identifier);
      Console.WriteLine("  Sequence: {0}", Sequence);
      string stringData = Encoding.ASCII.GetString(response.Message, 4, response.MessageSize - 4);
      Console.WriteLine("  data: {0}", stringData);
      
      host.Close();
   }
}

class ICMP
{
   public byte Type;
   public byte Code;
   public UInt16 Checksum;
   public int MessageSize;
   public byte[] Message = new byte[1024];

   public ICMP()
   {
   }

   public ICMP(byte[] data, int size)
   {
      Type = data[20];
      Code = data[21];
      Checksum = BitConverter.ToUInt16(data, 22);
      MessageSize = size - 24;
      Buffer.BlockCopy(data, 24, Message, 0, MessageSize);
   }

   public byte[] getBytes()
   {
      byte[] data = new byte[MessageSize + 9];
      Buffer.BlockCopy(BitConverter.GetBytes(Type)0, data, 01);
      Buffer.BlockCopy(BitConverter.GetBytes(Code)0, data, 11);
      Buffer.BlockCopy(BitConverter.GetBytes(Checksum)0, data, 22);
      Buffer.BlockCopy(Message, 0, data, 4, MessageSize);
      return data;
   }

   public UInt16 getChecksum()
   {
      UInt32 chcksm = 0;
      byte[] data = getBytes();
      int packetsize = MessageSize + 8;
      int index = 0;

      while index < packetsize)
      {
         chcksm += Convert.ToUInt32(BitConverter.ToUInt16(data, index));
         index += 2;
      }
      chcksm = (chcksm >> 16(chcksm & 0xffff);
      chcksm += (chcksm >> 16);
      return (UInt16)(~chcksm);
   }
}

วันพฤหัสบดีที่ 24 พฤศจิกายน พ.ศ. 2554

ยกเลิกการซ่อนเซลใน Excel ทั้งหมดภายใน 3 คลิ้ก

ยกเลิกการซ่อนเซลใน Excel ทั้งหมดภายใน 3 คลิ้ก

 
 
 
 
 
 
i
 
Rate This
Quantcast
ในกรณีที่มีการซ่อนเซลจำนวนมาก จนนับไม่ถ้วน การจะแสดงเซล (Unhide) เหล่านั้นทีละแถวหรือคอลัมน์ คงเป็นเรื่องยุ่งยากมากๆ แต่ผมมีวิธีลัดที่จะทำให้การแสดงเซลที่ซ่อนทั้งหมด Unhide เพียงแค่ 3 คลิ้กเท่านั้น ทำตามวิธีดังต่อไปนี้ได้เลยครับ

วันพฤหัสบดีที่ 17 พฤศจิกายน พ.ศ. 2554

c# โปรแกรม ping

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

class Program
{
        static void Main(string[] args)
        {
            Ping pingSender = new Ping();

            String data = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            int timeout = 120;

            Console.Write("Enter IP Address:");
            String ip = Console.ReadLine();
            PingReply reply = pingSender.Send(ip, timeout, buffer);

            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine("Address:{0}", reply.Address.ToString());
                Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
                Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
                Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
                Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
            }
            Console.ReadKey();
        }
}

วันจันทร์ที่ 14 พฤศจิกายน พ.ศ. 2554

Thai Android Apk Tips&Trick : วิธีการลง Custom Rom ให้ Samsung Galaxy Tab 10.1 (Overcome V1.2.1)


มาเจอกันอีก 1 บทความ Tips&Tricks ครับ บทความนี้จัดหนักนิดนึง มาลงรอมโม(Custom Rom)กันครับ เดี๋ยวจะอธิบายเป็นภาษาไทยแบบ step by step เลยนะครับ แต่ขอบอกไว้ก่อนนะครับ การลง Custom Rom อาจจะทำให้หมดประกันหรือทำอันตรายต่อเครื่องของท่านได้ ก่อนจะลง Custom Rom ให้  Galaxy tab 10.1  ควรพิจารณาความเสี่ยงในจุดนี้ด้วยครับ เมื่อเตรียมตัวเตรียมใจพร้อมแล้วก็ไปลุยกัน

1.ดาวน์โหลดเจ้าตัว Restock Rom ไปเก็บไว้ใน Computer ก่อนเลยครับ เพราะเราจะใช้เจ้า Rom ตัวนี้เป็นรอมพื้นฐานก่อนจะลง Rom ของ Overcome นะครับ
001001-P7500_KI1_Restock.zip

2.ระหว่างรอการดาวน์โหลด จะ Backup อะไรก็ Backup เตรียมไว้นะครับ เพราะถ้าลงรอมใหม่ ข้อมูลทุกอย่างจะหายหมดเลย และสำหรับใครที่ยังไม่ Kies (เป็น Synchronize Software ของ Samsung ดาวน์โหลดได้ที่ Kies Download) แนะนำให้ลง Kies เพื่อที่คอมพิวเตอร์จะได้มี ไดรเวอร์ของ Galaxy Tab 10.1 ของเราครับ
3. Extract(แตกไฟล์) 001001-P7500_KI1_Restock.zip  ออกมาครับ สมมุติ Extract ไว้ในโฟลเดอร์ 001001-P7500_KI1_Restock นะครับ ซึ่งใน Folder (สำหรับคนที่เคยลงรอมตัวอื่นอาจจะงงนะครับ ทำไมไม่มีไฟล์ PITแต่เดี๋ยว PIT ตามมาครับ)
4. ดาวโหลด GT-P7500_PIT.zip มาครับ แล้วเมื่อ Extract File ออกมา จะได้ไฟล์ android_fastboot_emmc_full_p4_16G_32G.pit ให้นำไปเก็บไว้ใน  001001-P7500_KI1_Restock นะครับ พักคอมพิวเตอร์ของเราไว้ก่อนครับ
5. คราวนี้มาดูที่ Galaxy Tab 10.1 ของเราครับ ทำการปิดเครื่องก่อนเลยครับ เมื่อเครื่องปิดสนิทแล้ว ให้กดปุ่ม Power + ปุ่มลดเสียง (ปุ่ม Volumn Control ด้านที่ติดกับปุ่ม Power นะครับ) ค้างไว้ครับ แล้วจะมีรูป

ซึ่งปุ่มลดเสียงจะเป็นตัวเลื่อน Cursor ให้เลื่อน Cursor ไปที่เจ้าหุ่นเขียว Android ตามรูปครับ และกดที่ปุ่มเพิ่มเสียงเพื่อเข้าไปครับ แล้วจะได้ดังรูป
และนำเจ้า Galaxy Tab 10.1 ของเราไปต่อกับคอมพิวเตอร์ผ่านสาย USB DATA CONNECTOR ที่เค้าแถมมาให้นะครับ
6. กลับมาที่คอมพิวเตอร์ของเราครับ เปิดโฟลเดอร์ 001001-P7500_KI1_Restock และเข้าไปเปิด Odin3 v1.85.exe ซึ่งะอยู่ในโฟลเดอร์ Odin3 v1.85 อีกทีครับ จะได้หน้าตาดังรูป

วันศุกร์ที่ 4 พฤศจิกายน พ.ศ. 2554

c# excel

add referent 
com->microsoft office 12.0 Object Libary
net ->mircrosoft office interrop.excel

###############################################################33


using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;

      // --- export เป็นไฟล์ Excel --------
        private void mergeCell(string incell, ref Excel.Application excelApp)
        {
            excelApp.get_Range(incell, Type.Missing).Merge(Type.Missing); 
        }


        private void bt_excel_Click(object sender, EventArgs e)
        {

            saveFileDialog1.Filter = "Excel File(*.xls)|*.xls";
            saveFileDialog1.ShowDialog();
            Excel.Range oResizeRange;
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            
            //----------------- หัวตาราง ---------------------------
            xlWorkSheet.Cells[1, 1] = "ข้อมูล แผนก: " + dgv_data[11, 0].Value.ToString() + "    หน่วยรถ: " + dgv_data[12, 0].Value.ToString();                ;
            xlWorkSheet.Cells[2, 1] = "พื้นที่  จังหวัด: " + dgv_data[7, 0].Value.ToString() + "    อำเภอ: " + dgv_data[6, 0].Value.ToString() + "    ตำบล: " + dgv_data[5, 0].Value.ToString();
            
            //--------ตัวหนา --------
            xlApp.get_Range("A1", "D1").Font.Bold = true;
            xlApp.get_Range("A4", "M4").Font.Bold = true;

            //-----ขนาดคอลัม ------------
            xlApp.get_Range("A4","A4").ColumnWidth = 20;
            xlApp.get_Range("B4", "B4").ColumnWidth = 15;
            xlApp.get_Range("E4", "F4").ColumnWidth = 15;
            xlApp.get_Range("G4", "G4").ColumnWidth = 15;
            xlApp.get_Range("J4", "K4").ColumnWidth = 15;
            //------marge cell --------
            mergeCell("A1:M1,A2:M2",ref xlApp);



            //------จัดกลาง---------
            xlApp.get_Range("A4", "M1").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
            xlApp.get_Range("A2", "M2").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;


            xlWorkSheet.Cells[4, 1] = "ชื่อร้าน";
            xlWorkSheet.Cells[4, 2] = "ผู้ติดต่อ";
            xlWorkSheet.Cells[4, 3] = "เลขที่";
            xlWorkSheet.Cells[4, 4] = "หมู่ที่";
            xlWorkSheet.Cells[4, 5] = "ถนน";
            xlWorkSheet.Cells[4, 6] = "ตำบล";
            xlWorkSheet.Cells[4, 7] = "อำเภอ";
            xlWorkSheet.Cells[4, 8] = "จังหวัด";
            xlWorkSheet.Cells[4, 9] = "รหัสไปรษณีย์";
            xlWorkSheet.Cells[4, 10] = "เบอร์ร้าน";
            xlWorkSheet.Cells[4, 11] = "มือถือ";
            xlWorkSheet.Cells[4, 12] = "Day Trip";
            xlWorkSheet.Cells[4, 13] = "วันเยี่ยม";

            //---- ตีเส้น -----------------------------------------------

            oResizeRange = xlApp.get_Range("a4", "a" + (dgv_data.RowCount + 4).ToString()).get_Resize(Missing.Value, 13);
            oResizeRange.Borders.Weight = Excel.XlBorderWeight.xlThin;

            // ----------------------------cell value format string ----------------------------
            xlApp.get_Range("J5", "J"+(dgv_data.RowCount + 4).ToString()).NumberFormat = "@";
            xlApp.get_Range("K5", "K" + (dgv_data.RowCount + 4).ToString()).NumberFormat = "@";
            xlApp.get_Range("C5", "C" + (dgv_data.RowCount + 4).ToString()).NumberFormat = "@";
         

            //--------------------------------------------------------
            //   xlApp.get_Range("A1:A360,B1:E1", Type.Missing).Merge(Type.Missing);

            //---------------------------------------------------
            
            
            
            int i = 0;
            int j = 0; 

            for (i = 0; i <= dgv_data.RowCount  - 1; i++)
            {
                xlApp.get_Range("A"+(i+5).ToString(), "M"+(i+5).ToString()).HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
                for (j = 0; j <= dgv_data.ColumnCount - 1; j++)
                {
                        
                        DataGridViewCell cell = dgv_data[j, i];
                        if ((j != 11) && (j != 12))
                        {
                            if ((cell.Value.ToString() == "-") || (cell.Value.ToString() == "0"))//ตรวจสอบข้อมูลที่ขาด
                            {
                                xlWorkSheet.Cells[i + 5, j + 1] = "";
                            }
                            else
                            {
                                xlWorkSheet.Cells[i + 5, j + 1] = cell.Value;
                            }
                        }
                    
                }
            }


            xlWorkBook.SaveAs(saveFileDialog1.FileName,Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);

            MessageBox.Show("Excel file created");
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

//--------------------------------------------------- end  excel -------------------------------------------

วันพฤหัสบดีที่ 3 พฤศจิกายน พ.ศ. 2554

c# การสร้าง List ข้อมูลเพื่อเป็น item ให้ combo box

ขั้นตอนที่1
  สร้างคลาสของข้อมูลตามโครงสร้างนี้
     public  class reportType
    {       string name;
            int value;
         
      public reportType(string inName, int inValue)
            {
                name = inName;
                this.value = inValue;
            }
            public string reportName
            {
                get
                {
                    return name;
                }
            }
            public int reportValue
            {
                get
                {
                    return value;
                }
            }
        }
 สร้าง list ข้อมูล
               List<reportType> list = new List<reportType>();
            list.Add(new reportType("A", 11));
            list.Add(new reportType("B", 12));
            list.Add(new reportType("C", 13));

ผูกกับ combo box
      cb_reportType.DisplayMember = "reportName";
       cb_reportType.ValueMember = "reportValue";
       cb_reportType.DataSource = list;

วันพุธที่ 2 พฤศจิกายน พ.ศ. 2554

c# กล่องข้อความยืนยัน

 DialogResult dlg = MessageBox.Show("คุณต้องการลบบิลเลขที่ \"" + dgv_delSaleType[0, this.dgv_delSaleType.CurrentCell.RowIndex].Value.ToString() + "\" ใช้หรือไม่", "ยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dlg == DialogResult.Yes){

               //---  คำสั่งเมื่อกดปุ่ม ok
           }

c# diable Stripmenu

::Menu  item ::
menuStrip1.Items[2].Visible = false;


::item::
แกไขขอมลToolStripMenuItem.Visible = false;

c# ปิด control bar

 this.ControlBox = false;

วันอังคารที่ 1 พฤศจิกายน พ.ศ. 2554

c# เข้ารหัส MD5

using System.Text;
using System.Security.Cryptography;

public void DoMD5(string input)
{
       string encrypted = [color=#4000FF]EnCryptToMD5[/color](txtpassword.text);
       Response.Write(encrypted );
}

public static string EnCryptToMD5(string input)
{
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
            bs = md5.ComputeHash(bs);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in bs)
            {
                sb.Append(b.ToString("x2").ToLower());
            }

            return sb.ToString();
}

c# การเรียกใช้ Store และ ดึงข้อมูลที่ได้จาก store

 private string loadNumData()
          {
            conDB conDB01 = new conDB();
            string conStr = conDB01.getConStr();
            SqlConnection conn = new SqlConnection(conStr);
            conn.Open();

            SqlCommand cmd = new SqlCommand("dbo.SP_getNumImdata", conn);

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@inDept", SqlDbType.VarChar).Value = lb_dept.Text;
            cmd.Parameters.Add("@inUnit", SqlDbType.NVarChar).Value = lb_unit.Text;

            SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            myAdapter.Fill(ds, "numData");
            conn.Close();

            if (ds.Tables["numData"].Rows.Count < 1)
            {
                return "No data";
            }
            else
            {
                return ds.Tables["numData"].Rows[0]["num"].ToString(); // ข้อมูลจาก store
            }
            }