C#:
前面補0的數字字串
String.Format("{0:0000}", 157); // 輸出 0157
 
前後都補0的數字字串
String.Format("{0:0000.0000}", 157.42); // 輸出 0157.4200
 
每3位數(千)加逗號
(String.Format("{0:0,0}", 38560); // 輸出 38,560
0:0  這樣表示會把前面補0 ,例如本來是6,會顯示06,所以不要有0: 就不會變成06

createps 發表在 痞客邦 留言(0) 人氣()

同步發表於Google Blogger
     Dictionary<string, object> tagDictionary = new Dictionary<string, object>();
        private void Form1_Load(object sender, EventArgs e)
        {
            
            tagDictionary.Add("AnimatedModelData",1000);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (tagDictionary["AnimatedModelData"] != null)
            {
                int i = (int)tagDictionary["AnimatedModelData"];
            }
        }

createps 發表在 痞客邦 留言(0) 人氣()

//=========================================================================================
不取得子目錄的話用這個:
string[] dirs = Directory.GetDirectories(@"c:\");//路徑
foreach (string dir in dirs)
{
  Console.WriteLine(dir);
}
文件的話把GetDirectories改成GetFiles
如果要遞歸的話,就是想取得此目錄下所有子目錄和文件的辦法用這個:
public void FindFile(string dir)             //參數為指定的目錄
{  
  //在指定目錄及子目錄下查找文件,在listBox1中列出子目錄及文件
  DirectoryInfo Dir=new DirectoryInfo(dir);
  try
  {
    foreach(DirectoryInfo d in Dir.GetDirectories())  //查找子目錄 
    {
      FindFile(Dir+d.ToString()+"\\");
      listBox1.Items.Add(Dir+d.ToString()+"\\");   //listBox1中填加目錄名
    }
    foreach(FileInfo f in Dir.GetFiles("*.*"))      //查找文件
    {
      listBox1.Items.Add(Dir+f.ToString());  //listBox1中填加文件名
    }
  }
  catch(Exception e)
  {
    MessageBox.Show(e.Message);
  }
}
調用
private void button1_Click(object sender, System.EventArgs e)
{
  string currentdir="c:\\testfolder";  //搜索的目錄
  if(currentdir[currentdir.Length-1]!='\\') //非根目錄
  currentdir+="\\"; 
  FindFile(currentdir);  //調用查找文件函數
}
注意 using System.IO;
//=========================================================================================

createps 發表在 痞客邦 留言(0) 人氣()

整理一下lock的特性。
1.lock的目標並不是物件,而是程式碼區段,只有被lock包覆的程式區段才會有作用。
2.當使用lock(xxx)時,xxx可以想象成是一個識別號,所有相同識別號的lock程式區段,會受到lock的影響。
3.lock並不能使用struct來當作識別號。
4.在大家都在存取"相同記憶體位置時" lock 這個標籤才有使用的意義.


from:http://www.dotblogs.com.tw/fphoenix/archive/2009/05/25/8557.aspx

createps 發表在 痞客邦 留言(0) 人氣()

C# 取得傳來的byte資料
從第3個BYTE開始
string[] 資料 = System.Text.Encoding.Unicode.GetString(位元陣列, 3, 位元陣列.Length - 3).Split('\t');
    byte[] s =Guid.NewGuid().ToByteArray();
            string c = System.Text.Encoding.Unicode.GetString(s);
            byte[] d = System.Text.Encoding.Unicode.GetBytes(c);
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == d[i])
                {
                    int k = 0;
                }
            }

createps 發表在 痞客邦 留言(0) 人氣()

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
            frm2.Show();//開啟子視窗
            this.Hide();//隱藏父視窗
        }
        void frm2_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Show();//子視窗關閉時,父視窗再顯示出來
        }

createps 發表在 痞客邦 留言(0) 人氣()

跨執行緒呼叫控制項
  在非同步概觀這篇文章提到如果在別的執行緒中直接操作Windows Form上的任何一個控制項,換句話說就是操作控制項的執行緒與建立控制項的執行緒並不是同一個,就會產生InvalidOperationException。這是.Net Framework中針對多執行緒工作所設下的一道安全性機制。
        執行緒使用委派
        private delegate void 委派_函式(string data);
        private void 函式(string data)
        {
            if (this.InvokeRequired)
            {
                委派_函式 a = new 委派_函式(函式);
                this.Invoke(a,data);
            }
            else
            {
                this.textbox.Text = data;
            }
        }

createps 發表在 痞客邦 留言(0) 人氣()

DateTime.Now.ToString("HH:mm:ss MM月dd日yyyy年");
HH:24小時制
hh:12小時制
MM:月份
mm:分鐘

createps 發表在 痞客邦 留言(0) 人氣()

            this.listView1 = new System.Windows.Forms.ListView();
            // 
            // listView1
            // 
            this.listView1.Location = new System.Drawing.Point(26, 119);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(304, 97);
            this.listView1.TabIndex = 6;
            this.listView1.UseCompatibleStateImageBehavior = false;
            this.listView1.View = System.Windows.Forms.View.Details;//這個是重點,有這個才會顯示Columns的

createps 發表在 痞客邦 留言(0) 人氣()

C# 開啟執行檔
開啟執行檔
System.Diagnostics.Process.Start("notepad.exe"); // 記事本
System.Diagnostics.Process.Start("calc.exe"); // 小算盤
後面加參數
System.Diagnostics.Process.Start("自己寫的執行檔.exe","自己需要的參數");
再配合主程式
 static void Main(string[] 自己需要的參數)
{
            if (自己需要的參數[0] != "asldfie09484")
                return;
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
}

createps 發表在 痞客邦 留言(0) 人氣()


class Calendar1
{
    public const int months = 12;
}
class Calendar2
{
    const int months = 12, weeks = 52, days = 365;
}
class Calendar3
{
    const int months = 12;
    const int weeks = 52;
    const int days = 365;
    const double daysPerWeek = days / weeks;
    const double daysPerMonth = days / months;
}
常數可以標記為 public、private、protected、internal 或 protectedinternal。這些存取修飾詞將定義類別使用者如何存取常數。如需詳細資訊,請參閱存取修飾詞 (C# 程式設計手冊)。
常數會當做靜態欄位存取,但不能使用 static 關鍵字。若運算式不在定義常數的類別中,就必須使用類別名稱、句號和常數名稱來存取常數。例如:

createps 發表在 痞客邦 留言(0) 人氣()

  • Oct 05 Tue 2010 10:57
  • C# try


         try
        {
            string s = null;
            ProcessString(s);
        }
        catch (Exception e)
        {
            Console.WriteLine("{0} Exception caught.", e);
        }

createps 發表在 痞客邦 留言(0) 人氣()

Blog Stats
⚠️

成人內容提醒

本部落格內容僅限年滿十八歲者瀏覽。
若您未滿十八歲,請立即離開。

已滿十八歲者,亦請勿將內容提供給未成年人士。