Base64 Encode/Decode

ธันวาคม 16th, 2009

static public string EncodeTo64(string toEncode)

{

byte[] toEncodeAsBytes

= System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);

string returnValue

= System.Convert.ToBase64String(toEncodeAsBytes);

return returnValue;

}

static public string DecodeFrom64(string encodedData)

{

byte[] encodedDataAsBytes

= System.Convert.FromBase64String(encodedData);

string returnValue =

System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);

return returnValue;

}

————————

string myData = “Here is a string to encode.”;

string myDataEncoded = EncodeTo64(myData);

Console.WriteLine(myDataEncoded);

string myDataUnencoded = DecodeFrom64(myDataEncoded);

Console.WriteLine(myDataUnencoded);

Console.ReadLine();

C#

Twitter api sample

ธันวาคม 9th, 2009
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twitterizer.Framework;

namespace TwitterSample
{
    class Program
    {
        static void Main(string[] args)
        {
            if (Twitter.VerifyCredentials("twitter_user", "twitter_password"))
            {
                Twitter twitter = new Twitter("twitter_user", "twitter_password", "my_app");

                PrintReplies(twitter);
                PrintFollowers(twitter);
                PrintFriends(twitter);
                PrintDirectMessages(twitter);
                PrintFriendsTimeline(twitter);
                PrintPublicTimeline(twitter);
                PrintUserTimeline(twitter);

                //post a message
                twitter.Status.Update("Testing 123, Testing");
            }
            Console.ReadKey();
        }

        public static TwitterUser GetUserInfo(Twitter twitter, string userId)
        {
            TwitterUser user = twitter.Status.Show(userId);

            return user;
        }

        public static void PrintReplies(Twitter twitter)
        {
            TwitterStatusCollection col = twitter.Status.Replies();

            Console.WriteLine("Replies");

            foreach (TwitterStatus status in col)
            {
                Console.WriteLine("------------------------------------------");
                Console.WriteLine("Created:                " + status.Created);
                Console.WriteLine("ID:                     " + status.ID);
                Console.WriteLine("InReplyToStatusID:      " + status.InReplyToStatusID);
                Console.WriteLine("InReplyToStatusID:      " + status.InReplyToUserID);
                Console.WriteLine("IsFavorited:            " + status.IsFavorited);
                Console.WriteLine("IsTruncated:            " + status.IsTruncated);
                Console.WriteLine("RecipientID:            " + status.RecipientID);
                Console.WriteLine("Source:                 " + status.Source);
                Console.WriteLine("Text:                   " + status.Text);
                Console.WriteLine("TwitterUser.ScreenName: " + status.TwitterUser.ScreenName);
            }
        }

        public static void PrintFollowers(Twitter twitter)
        {
            TwitterUserCollection col = twitter.User.Followers();

            Console.WriteLine("Followers");

            foreach (TwitterUser user in col)
            {
                Console.WriteLine("------------------------------------------");
                Console.WriteLine("Description:       " + user.Description);
                Console.WriteLine("Friends_count:     " + user.Friends_count);
                Console.WriteLine("ID:                " + user.ID);
                Console.WriteLine("IsProtected:       " + user.IsProtected);
                Console.WriteLine("Location:          " + user.Location);
                Console.WriteLine("NumberOfFollowers: " + user.NumberOfFollowers);
                Console.WriteLine("ProfileImageUri:   " + user.ProfileImageUri);
                Console.WriteLine("ProfileUri:        " + user.ProfileUri);
                Console.WriteLine("ScreenName:        " + user.ScreenName);
                if (user.Status != null)
                {
                    Console.WriteLine("Status.Text:       " + user.Status.Text);
                }
                Console.WriteLine("TimeZone:          " + user.TimeZone);
                Console.WriteLine("UserName:          " + user.UserName);
            }
        }

        public static void PrintFriends(Twitter twitter)
        {
            TwitterUserCollection col = twitter.User.Friends();

            Console.WriteLine("Friends");

            foreach (TwitterUser user in col)
            {
                Console.WriteLine("------------------------------------------");
                Console.WriteLine("Description:       " + user.Description);
                Console.WriteLine("Friends_count:     " + user.Friends_count);
                Console.WriteLine("ID:                " + user.ID);
                Console.WriteLine("IsProtected:       " + user.IsProtected);
                Console.WriteLine("Location:          " + user.Location);
                Console.WriteLine("NumberOfFollowers: " + user.NumberOfFollowers);
                Console.WriteLine("ProfileImageUri:   " + user.ProfileImageUri);
                Console.WriteLine("ProfileUri:        " + user.ProfileUri);
                Console.WriteLine("ScreenName:        " + user.ScreenName);
                if (user.Status != null)
                {
                    Console.WriteLine("Status.Text:       " + user.Status.Text);
                }
                Console.WriteLine("TimeZone:          " + user.TimeZone);
                Console.WriteLine("UserName:          " + user.UserName);
            }
        }

        public static void PrintDirectMessages(Twitter twitter)
        {
            TwitterStatusCollection col = twitter.DirectMessages.DirectMessages();

            Console.WriteLine("DirectMessages");

            foreach (TwitterStatus status in col)
            {
                Console.WriteLine("------------------------------------------");
                Console.WriteLine("Created:                " + status.Created);
                Console.WriteLine("ID:                     " + status.ID);
                Console.WriteLine("InReplyToStatusID:      " + status.InReplyToStatusID);
                Console.WriteLine("InReplyToStatusID:      " + status.InReplyToUserID);
                Console.WriteLine("IsFavorited:            " + status.IsFavorited);
                Console.WriteLine("IsTruncated:            " + status.IsTruncated);
                Console.WriteLine("RecipientID:            " + status.RecipientID);
                Console.WriteLine("Source:                 " + status.Source);
                Console.WriteLine("Text:                   " + status.Text);
                Console.WriteLine("TwitterUser.ScreenName: " + status.TwitterUser.ScreenName);
            }
        }

        public static void PrintFriendsTimeline(Twitter twitter)
        {
            TwitterStatusCollection col = twitter.Status.FriendsTimeline();

            Console.WriteLine("FriendsTimeline");

            foreach (TwitterStatus status in col)
            {
                Console.WriteLine("------------------------------------------");
                Console.WriteLine("Created:                " + status.Created);
                Console.WriteLine("ID:                     " + status.ID);
                Console.WriteLine("InReplyToStatusID:      " + status.InReplyToStatusID);
                Console.WriteLine("InReplyToStatusID:      " + status.InReplyToUserID);
                Console.WriteLine("IsFavorited:            " + status.IsFavorited);
                Console.WriteLine("IsTruncated:            " + status.IsTruncated);
                Console.WriteLine("RecipientID:            " + status.RecipientID);
                Console.WriteLine("Source:                 " + status.Source);
                Console.WriteLine("Text:                   " + status.Text);
                Console.WriteLine("TwitterUser.ScreenName: " + status.TwitterUser.ScreenName);
            }
        }

        public static void PrintPublicTimeline(Twitter twitter)
        {
            TwitterStatusCollection col = twitter.Status.PublicTimeline();

            Console.WriteLine("PublicTimeline");

            foreach (TwitterStatus status in col)
            {
                Console.WriteLine("------------------------------------------");
                Console.WriteLine("Created:                " + status.Created);
                Console.WriteLine("ID:                     " + status.ID);
                Console.WriteLine("InReplyToStatusID:      " + status.InReplyToStatusID);
                Console.WriteLine("InReplyToStatusID:      " + status.InReplyToUserID);
                Console.WriteLine("IsFavorited:            " + status.IsFavorited);
                Console.WriteLine("IsTruncated:            " + status.IsTruncated);
                Console.WriteLine("RecipientID:            " + status.RecipientID);
                Console.WriteLine("Source:                 " + status.Source);
                Console.WriteLine("Text:                   " + status.Text);
                Console.WriteLine("TwitterUser.ScreenName: " + status.TwitterUser.ScreenName);
            }
        }

        public static void PrintUserTimeline(Twitter twitter)
        {
            TwitterStatusCollection col = twitter.Status.UserTimeline();

            Console.WriteLine("UserTimeline");

            foreach (TwitterStatus status in col)
            {
                Console.WriteLine("------------------------------------------");
                Console.WriteLine("Created:                " + status.Created);
                Console.WriteLine("ID:                     " + status.ID);
                Console.WriteLine("InReplyToStatusID:      " + status.InReplyToStatusID);
                Console.WriteLine("InReplyToStatusID:      " + status.InReplyToUserID);
                Console.WriteLine("IsFavorited:            " + status.IsFavorited);
                Console.WriteLine("IsTruncated:            " + status.IsTruncated);
                Console.WriteLine("RecipientID:            " + status.RecipientID);
                Console.WriteLine("Source:                 " + status.Source);
                Console.WriteLine("Text:                   " + status.Text);
                Console.WriteLine("TwitterUser.ScreenName: " + status.TwitterUser.ScreenName);
            }
        }
    }
}

C#

ฟังก์ชั่น หานามสกุลของไฟล์ ด้วยภาษา php

กันยายน 8th, 2009

function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split(”[/\\.]“, $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}

PHP

สร้าง class แบบ dynamic

กันยายน 2nd, 2009
using System;  
using System.Diagnostics;  
class Foo { }  
static class Program  
{  
    static void Main()  
    {  
        Type type = typeof(Foo);  
        const int count = 5000000;
        GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
        RunTestWithActivator(type, count);
        GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
        typeof(Program).GetMethod(“RunTestWithGenerics”).  
            MakeGenericMethod(type).Invoke(nullnew object[] { count });  
    }  
    public static void RunTestWithActivator(Type type, int count)  
    {  
        Stopwatch watch = Stopwatch.StartNew();  
        for (int i = 0; i < count; i++)  
        {  
            object obj = Activator.CreateInstance(type);  
        }  
        watch.Stop();  
        Console.WriteLine(“With Activator: {0}”, watch.ElapsedMilliseconds);  
    }  
    public static void RunTestWithGenerics<T>(int count) where T : new() {  
        Stopwatch watch = Stopwatch.StartNew();  
        for (int i = 0; i < count; i++)  
        {  
            T t = new T();  
        }  
        watch.Stop();  
        Console.WriteLine(“With generics: {0}”, watch.ElapsedMilliseconds);  
    }  

C#

C# อ่านเขียน app.config ของโปรแกรม

สิงหาคม 25th, 2009

อ่าน

string value = ConfigurationManager.AppSettings["appvalue"]; 

เขียน

System.Configuration.Configuration config =ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);   

config.AppSettings.Settings["appvalue"].Value = “3″;        

config.Save(ConfigurationSaveMode.Modified);   

ConfigurationManager.RefreshSection(“appSettings”);  

 

C#

แสดง path เต็มของไฟล์บน web server

สิงหาคม 20th, 2009

เป็นโค้ดสั้นๆ ที่เพิ่งจะค้นพบ

<?php
$fullpath = getcwd();
echo
“Full path is $fullpath”;
?>

PHP

php แสดงรายการไฟล์ใน directory

มีนาคม 2nd, 2009
<html>
<head>
<title>PHP opendir()</title>
</head>
<body>
<?
$objOpen = opendir(”./”);
while (($file = readdir($objOpen)) !== false)
{
echo “filename: ” . $file . “<br />”;
}
?>
</body>
</html>

PHP

.NET Compact Framework (PDA) ติดต่อ Mssql

กุมภาพันธ์ 6th, 2009

ในการติดต่อกับฐานข้อมูลของ microsoft เอง จะใช้ tools ที่ชื่อว่า Microsoft SQL Server 2005 Mobile Edition Device SDK

download ได้จาก

http://www.microsoft.com/downloads/details.aspx?FamilyID=5bd8abaa-5813-4db3-b23a-24551de2ecc1&displaylang=en

ผมเคยแค่ต่อ db ที่เป็น .sdf บนเครื่อง pda ครับ ยังไม่เคยลอง remote ต่อไปยัง db ที่เป็น ip ท่านใดลองได้แล้ว รบกวนแจ้งวิธีการหน่อยนะครับ

MOBILE

.NET Compact Framework (PDA) ติดต่อ Mysql

กุมภาพันธ์ 5th, 2009

ใช้ connector ที่ Mysql มีมาให้ครับ

http://dev.mysql.com/downloads/connector/net/5.1.html

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace MySQLCompact
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
try
{
lBoxResults.Items.Clear();
MySqlConnection connection = new
MySqlConnection(”SERVER=MySQL server IP
address;DATABASE=databaseName;UID=user;PASSWORD=password;pooling=false”);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = “select * from table1″;
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
string thisrow = “”;
for (int i = 0; i < Reader.FieldCount; i++)
thisrow += Reader.GetValue(i).ToString() +
lBoxResults.Items.Add(thisrow);
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

MOBILE

แก้ปัญหาต่อ Mysql ด้วย DBDesigner4 ไม่ได้

กุมภาพันธ์ 3rd, 2009

ผมได้ทดสอบใช้ DBDesigner4. แต่เกิด Error ในขั้นตอนการติดต่อ MySql

Connection to database failed.

dbExpress Error: Invalid
Username/Password

แก้ไขโดย

>mysql -u root -p
>SET PASSWORD FOR ‘root’@'localhost’ = OLD_PASSWORD(’r00tp45sw0rd’);
>UPDATE mysql.user SET Password = OLD_PASSWORD(’r00tp45sw0rd’) WHERE Host = ‘localhost’ AND User = ‘root’;
>FLUSH PRIVILEGES;

OpenSource