C# Snippets

Discussion in 'Source Code' started by Diebstahl, Nov 27, 2014.

  1. Diebstahl

    Diebstahl Newbie

    Joined:
    Nov 5, 2014
    Messages:
    4
    Likes Received:
    0
    [font='verdana,geneva,sans-serif']Ich werde hier ab und zu ein paar CodeSnippets für C# posten. Die einen mehr und die anderen weniger nützlich.

    ______________________________________________________________________________________________________________________________

    Dieses Snippet ermöglicht es bei dem (kostenlosen) VPN Anbieter "VPNBook" das Passwort auszulesen und damit anschließend eine bestehende VPN Verbindung unter Windows aufzubauen.[/font]

    Code:
    public static void connection()
    {
        var webseite = "http://www.vpnbook.com/freevpn";
        var searchWithinThis = (getHTML(webseite));
        var searchForThis = "Password";
        var firstCharacter = searchWithinThis.IndexOf(searchForThis);
        var pass = searchWithinThis.Substring(firstCharacter + 20, firstCharacter + 8 - firstCharacter);           
    
        System.Diagnostics.Process.Start("rasdial.exe", "VPNNAME USER "+pass);
    }               
    
    private static string getHTML(string url)
    {            
        var request = (HttpWebRequest)HttpWebRequest.Create(url);       
        var response = (HttpWebResponse)request.GetResponse();             
        var stream = new StreamReader(response.GetResponseStream());          
        var quelltext = stream.ReadToEnd();  
    
        stream.Close();
        response.Close();
    
        return quelltext;
    }

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    [font='verdana,geneva,sans-serif']
    Mit diesem Snippet kann man die eigene IPv4-Addresse via "http://checkip.dyndns.org" ermitteln.
    [/font]
    Code:
    private string GetPublicIPv4()
    {
        try
        {
            string url = "http://checkip.dyndns.org";
            System.Net.WebRequest req = System.Net.WebRequest.Create(url);
            System.Net.WebResponse resp = req.GetResponse();
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            string response = sr.ReadToEnd().Trim();
    
            string[] a = response.Split(':');
            string a2 = a[1].Substring(1);
            string[] a3 = a2.Split('<');
            string a4 = a3[0];
    
            return a4;
        }
        catch (Exception)
        {
            return null; //Hier ggf. etwas sinnvolles einfügen und spezifische Exception abfangen.
        }
    }

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    [font='verdana,geneva,sans-serif']Zur Ermittelung der eigenen IPv6-Addresse. Evtl. muss es angepasst werden, aber sollte kein Problem darstellen.[/font]

    Code:
    private string GetPublicIPv6()
    {
        try
        {
            string strHostName = System.Net.Dns.GetHostName(); ;
            IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
            IPAddress[] addr = ipEntry.AddressList;
    
            if (addr[0].AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
            {
                return addr[0].ToString();
            }
            return null;
        }
        catch (Exception)
        {
            return null;
        }
    }

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    [font='verdana,geneva,sans-serif']Kleiner Snippet um eine CSV-Datei einer DataTable hinzuzufügen. Nicht die schönste Variante, aber eine funktionelle.[/font]

    Code:
    private DataTable GetTableFromCSV(string path)
    {
        if (!File.Exists(path))
            throw new FileNotFoundException();
       
        FileInfo fileInfo = new FileInfo(path);
        DataTable dataTable = new DataTable();
    
        string connectionString = String.Format("Driver={{Microsoft Text Driver (*.txt; *.csv)}};Dbq={0};", fileInfo.DirectoryName);
    
        OdbcConnection connection = new OdbcConnection(connectionString);
        OdbcDataAdapter da = new OdbcDataAdapter(String.Format("select * from [{0}]",fileInfo.Name), connection);
    
        da.Fill(dataTable);
    
        return dataTable;
    }

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Hier ist meine KeyboardHook-Class. Ab und zu recht nützlich, wenn man nicht für jedes Projekt einen neuen erstellen möchte.

    Code:
    class KeyboardHook
        {
            private static class API
            {
                [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
                public static extern IntPtr SetWindowsHookEx(int idHook, HookDel lpfn, IntPtr hMod, uint dwThreadId);
    
                [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
                [return: MarshalAs(UnmanagedType.Bool)]
                public static extern bool UnhookWindowsHookEx(IntPtr hhk);
    
                [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
                public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
    
                [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
                public static extern IntPtr GetModuleHandle(string lpModuleName);
    
            }
    
            public enum VK
            {
                VK_LBUTTON = 0X01,
                VK_RBUTTON = 0X02,
                VK_MBUTTON = 0X04,
    
                VK_BACK = 0X08,
                VK_TAB = 0X09,
                VK_RETURN = 0X0D,
                VK_SHIFT = 0X10,
                VK_CONTROL = 0X11,
                VK_PAUSE = 0X13,
                VK_CAPITAL = 0X14,
                VK_ESCAPE = 0X1B,
                VK_SPACE = 0X20,
                VK_PRIOR = 0X21,
                VK_NEXT = 0X22,
                VK_END = 0X23,
                VK_HOME = 0X24,
                VK_LEFT = 0X25,
                VK_UP = 0X26,
                VK_RIGHT = 0X27,
                VK_DOWN = 0X28,
                VK_SNAPSHOT = 0X2C,
                VK_INSERT = 0X2D,
                VK_DELETE = 0X2E,
    
                VK_0 = 0X30,
                VK_1 = 0X31,
                VK_2 = 0X32,
                VK_3 = 0X33,
                VK_4 = 0X34,
                VK_5 = 0X35,
                VK_6 = 0X36,
                VK_7 = 0X37,
                VK_8 = 0X38,
                VK_9 = 0X39,
    
                VK_A = 0X41,
                VK_B = 0X42,
                VK_C = 0X43,
                VK_D = 0X44,
                VK_E = 0X45,
                VK_F = 0X46,
                VK_G = 0X47,
                VK_H = 0X48,
                VK_I = 0X49,
                VK_J = 0X4A,
                VK_K = 0X4B,
                VK_L = 0X4C,
                VK_M = 0X4D,
                VK_N = 0X4E,
                VK_O = 0X4F,
                VK_P = 0X50,
                VK_Q = 0X51,
                VK_R = 0X52,
                VK_S = 0X53,
                VK_T = 0X54,
                VK_U = 0X55,
                VK_V = 0X56,
                VK_W = 0X57,
                VK_X = 0X58,
                VK_Y = 0X59,
                VK_Z = 0X5A,
    
                VK_NUMPAD0 = 0X60,
                VK_NUMPAD1 = 0X61,
                VK_NUMPAD2 = 0X62,
                VK_NUMPAD3 = 0X63,
                VK_NUMPAD4 = 0X64,
                VK_NUMPAD5 = 0X65,
                VK_NUMPAD6 = 0X66,
                VK_NUMPAD7 = 0X67,
                VK_NUMPAD8 = 0X68,
                VK_NUMPAD9 = 0X69,
                VK_ADD = 0X6B,
                VK_SEPERATOR = 0X6C,
                VK_SUBTRACT = 0X6D,
                VK_DECIMAL = 0X6E,
                VK_DIVIDE = 0X6F,
    
                VK_F1 = 0X70,
                VK_F2 = 0X71,
                VK_F3 = 0X72,
                VK_F4 = 0X73,
                VK_F5 = 0X74,
                VK_F6 = 0X75,
                VK_F7 = 0X76,
                VK_F8 = 0X77,
                VK_F9 = 0X78,
                VK_F10 = 0X79,
                VK_F11 = 0X7A,
                VK_F12 = 0X7B,
    
                VK_NUMLOCK = 0X90,
                VK_SCROLL = 0X91,
                VK_LSHIFT = 0XA0,
                VK_RSHIFT = 0XA1,
                VK_LCONTROL = 0XA2,
                VK_RCONTROL = 0XA3,
            }
    
            public delegate IntPtr HookDel( int nCode, IntPtr wParam, IntPtr lParam);
    
            public delegate void KeyHandler(IntPtr wParam, IntPtr lParam);
    
            private static IntPtr hhk = IntPtr.Zero;
            private static HookDel hd;
            private static KeyHandler kh;
    
            public static void CreateHook(KeyHandler _kh)
            {
                Process _this = Process.GetCurrentProcess();
                ProcessModule mod = _this.MainModule;
                hd = HookFunc;
                kh = _kh;
    
                hhk = API.SetWindowsHookEx(13, hd, API.GetModuleHandle(mod.ModuleName), 0);
            }
    
            public static bool DestroyHook()
            {
                return API.UnhookWindowsHookEx(hhk);
            }
    
            private static IntPtr HookFunc(int nCode, IntPtr wParam, IntPtr lParam)
            {
                int iwParam = wParam.ToInt32();
                if (nCode >= 0 && (iwParam == 0x100 || iwParam == 0x104)) kh(wParam, lParam);
    
                return API.CallNextHookEx(hhk, nCode, wParam, lParam);
            }
        }
    
    
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.