C#常用实例

1 時間

1.1 顯示在走的時間

控件:TextBox為顯示日期時間,命名為txtDateTimer

Timer為時鐘,命名為time

private void dtDateTimer_Tick(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            txtDateTimer.Text = dt.ToString();
     }

clip_image001

    設計畫面                             執行畫面

1.2 獲取本機時間與日期

控件:Label為顯示時間與日期的控件,命名為:lblDateTime

RadioButton控件,分別為選擇顯示時間與日期的控件,分別命名為showDate和showTime

Button為顯示時間日期按鈕,命名為btnShow

private void btnShow_Click(object sender, EventArgs e)
        {
            if (showDate.Checked)
                lblDateTime.Text = DateTime.Now.Year + "" + DateTime.Now.Month + "" + DateTime.Now.Day + "";
            if (showTime.Checked)
                lblDateTime.Text = DateTime.Now.Hour + "" + DateTime.Now.Minute + "" + DateTime.Now.Second + "";
        }

clip_image001[5]

    設計畫面                         執行畫面

1.3 比較時間的大小及計算天數

控件:Button為比較日期按鈕,命名為btnBJ

DateTimePicker為顯示所選的日期,命名為dtpKS和dtpJS

Label為顯示說明文字

private void btnBJ_Click(object sender, EventArgs e)
        {
            string strOne = dtpOne.Text;
            string strTwo = dtpTwo.Text;
            DateTime dtOne = Convert.ToDateTime(strOne);
            DateTime dtTwo = Convert.ToDateTime(strTwo);
            if (DateTime.Compare(dtOne, dtTwo) > 0)
            {
                txtTime.Text = "比較日期:" + strOne + " VS " + strTwo + "
" + "比較結果:" + strOne + ">" + strTwo + "
" + "相差天數:";
            }
            else if (DateTime.Compare(dtOne, dtTwo) < 0)
            {
                txtTime.Text = "比較日期:" + strOne + " VS " + strTwo + "
" + "比較結果:" + strOne + "<" + strTwo + "
" + "相差天數:";
            }
            else if (DateTime.Compare(dtOne, dtTwo) == 0)
            {
                txtTime.Text = "比較日期:" + strOne + " VS " + strTwo + "
" + "比較結果:" + strOne + "=" + strTwo + "
" + "相差天數:";
            }
        TimeSpan ts = dtTwo - dtOne; //計算天數
            txtTime.Text += ts.Days.ToString() + "" + "
";
        }

clip_image001[7]

    設計畫面                                                  執行畫面

2 文件夾:

添加命名空間:using System.IO;

2.1 創建文件夾和刪除文件夾及數量

控件:Button為按鈕,分別為創建及刪除按鈕,命名為btnCreate,btnDelete

TextBox為文本,分別名稱及數量,命名為txtNumber和txtName

Label為顯示說明文字

private void btnCreate_Click(object sender, EventArgs e)
        {
            if (txtNumber.Text == "" || txtName.Text == "")
            {
                MessageBox.Show("請輸入創建文件夾數量及名稱"); return;
            }
            int numble = Convert.ToInt32(txtNumber.Text);
            for (int i = 1; i <= numble; i++)
            {
                Directory.CreateDirectory("E:\Temp\" + txtName.Text + i.ToString());//創建語句,在E盤Temp下創建文件夾
            }
            MessageBox.Show("創建成功!");
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
             if (txtNumber.Text == "" || txtName.Text == "")
             {
                 MessageBox.Show("請輸入刪除文件夾的個數");
                 return;
             }
             int j = Convert.ToInt32(txtNumber.Text);
             for (int i = 1; i <= j; i++)
             {
                 Directory.Delete("E:\Temp\" + txtName.Text + i.ToString());//刪除語句,在E盤Temp下創建文件夾
             }
             MessageBox.Show("刪除完成");
}

clip_image001[9]

    設計畫面                                      執行畫面

2.2 獲取文件路徑:

控件:Button為獲取路徑按鈕,命名為btnPath

TextBox為顯示文件地址,命名為txtShow

private void btnPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();
            txtShow.Text = fbd.SelectedPath;
        }

clip_image001[11]

      設計畫面                                      執行畫面

3 文本

3.1 創建及刪除文本【E盤中創建及刪除zqy文本】

控件:Button為按鈕,分別為創建及刪除按鈕,命名為btn_Create,btn_Delete

private void btn_Create_Click(object sender, EventArgs e)
        {
            if (!File.Exists("E:\zqy.txt"))
            {
                FileStream fs1 = new FileStream("E:\zqy.txt", FileMode.Create, FileAccess.Write);//
                fs1.Close();
                MessageBox.Show("E:\zqy.txt" + "文本" + "創建成功", "提示");
            }
            else
                MessageBox.Show("E:\zqy.txt" + "文本已存在!", "提示");
       }
        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (File.Exists("E:\zqy.txt"))
            {
                FileInfo FI = new FileInfo("E:\zqy.txt");
                FI.Delete();
                MessageBox.Show("E:\zqy.txt" + "文本" + "刪除成功", "提示");
            }
            else
                MessageBox.Show("E:\zqy.txt" + "文本不存在!", "提示");
}

clip_image001[13]

    設計畫面                            執行畫面

3.2 複製及粘貼

控件:Button為複製,粘貼按鈕,命名為btnCopy和btnPaste

TextBox為複製,粘貼文本,命名為txtCopy和txtpaste

private void btnCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(txtCopy.Text);
        }
        private void btnPaste_Click(object sender, EventArgs e)
        {
            IDataObject iData = Clipboard.GetDataObject();
            if (txtCopy.Text != "")
            {
                txtpaste.Text += (String)iData.GetData(DataFormats.Text) + "
";
            }
            else  MessageBox.Show("沒有複製要粘貼的文本","提示");
}

clip_image001[15]

    設計畫面                             執行畫面

原文地址:https://www.cnblogs.com/ymtianyu/p/5590581.html