2010年2月11日 星期四

[C#] Region 註解


用來 Mark 大範圍程式碼 區塊 ,識別使用





使用後效果


[C#] enum 列舉

剛剛伙伴教我的,第一次使用

用來做流程控制最好了,而且還支援中文  :)

2010年2月3日 星期三

[C#] FTP 下載單一檔案(WebClient)

網路上有說,使用 WebClient 的作法碰到取很大檔案時 會有 timeout 問題,
目前自己測試 16MB 單檔是沒有問題。

using System.Net;
using System.IO;
----------------------------------
WebClient RMS = new WebClient();
RMS.Credentials = new NetworkCredential("ubuntu", "zzzzzz");
string FILE_NAME = "新增文字文件.txt";
byte[] fildData = RMS.DownloadData("ftp://10.1.1.195/tools/"+FILE_NAME);
FileStream file = File.Create(@"C:\Users\kuoming\Desktop\" + FILE_NAME);
file.Write(fildData, 0, fildData.Length);
file.Close();
listBox1.Items.Add(FILE_NAME);


完整程式碼:

 
 
 
 
 
using System;


using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Net;

using System.IO;



namespace _20100203_FTP_Client_test

{

public partial class Form1 : Form

{

string FILE_NAME = "";

public Form1()

{

InitializeComponent();

}



private void Form1_Load(object sender, EventArgs e)

{

this.Text= "目前目錄 為 10.1.1.195 / TOOLS ";

button1.Text = "Download";

listBox1.Items.AddRange(GetFileList());

listBox1.Sorted = true;

}



private void GET_FILE()

{

try

{

WebClient RMS = new WebClient();

RMS.Credentials = new NetworkCredential("ubuntu", "zzzzzz");

byte[] fildData = RMS.DownloadData("ftp://10.1.1.195/tools/" + FILE_NAME);

FileStream file = File.Create(@"C:\Users\kuoming\Desktop\" + FILE_NAME);

file.Write(fildData, 0, fildData.Length);

file.Close();

MessageBox.Show("OK");

}

catch (Exception QQ)

{

MessageBox.Show("目前不支援目錄下載...");

}



}



public string[] GetFileList()

{

string[] downloadFiles;

StringBuilder result = new StringBuilder();

FtpWebRequest reqFTP;

try

{

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + "10.1.1.195" + "/tools/"));

reqFTP.UseBinary = true;

reqFTP.Credentials = new NetworkCredential("ubuntu", "zzzzzz");

reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;

WebResponse response = reqFTP.GetResponse();

StreamReader reader = new StreamReader(response.GetResponseStream());

string line = reader.ReadLine();

while (line != null)

{

result.Append(line);

result.Append("\n");

line = reader.ReadLine();

}

result.Remove(result.ToString().LastIndexOf('\n'), 1);

reader.Close();

response.Close();

return result.ToString().Split('\n');

}

catch (Exception ex)

{

System.Windows.Forms.MessageBox.Show(ex.Message);

downloadFiles = null;

return downloadFiles;

}

}



private void button1_Click(object sender, EventArgs e)

{

FILE_NAME = listBox1.SelectedItem.ToString();

GET_FILE();

}



}

}

2010年1月26日 星期二

[Other] Firefox Javascript 可以只顯示文字的語法

有時候遇到很多圖片的網頁,可是只是想要 看文字的部分。所以把文字放大,背景顏色套用白色
 

可以存成『我的最愛』連結,當按下連結就可以套用了。

javascript:(function(){var%20newSS,%20styles='*%20{%20background:%20white%20!%20important;%20color:%20black%20!important;%20line-height:%20160%%20!important;%20font-size:%2018pt%20!important%20}%20:link,%20:link%20*%20{%20color:%20#0000EE%20!important%20}%20:visited,%20:visited%20*%20{%20color:%20#551A8B%20!important%20}';%20if(document.createStyleSheet)%20{%20document.createStyleSheet(%22javascript:'%22+styles+%22'%22);%20}%20else%20{%20newSS=document.createElement('link');%20newSS.rel='stylesheet';%20newSS.href='data:text/css,'+escape(styles);%20document.getElementsByTagName(%22head%22)[0].appendChild(newSS);%20}%20})();
  
原本樣式

          








套用後樣式








2010年1月23日 星期六

[C#] Listview

  

Listview 是很好用,之前一直都用 Listbox 來做,最近才開始接觸。。

 

   1: listView1.Clear(); //清空 listview



   2:            listView1.View = View.Details; //設定模式為 清單模式



   3:            listView1.GridLines = true; //繪製 格線



   4:            listView1.FullRowSelect=true; //ture 一次選擇一行  ; false 一次選一格 



   5:            listView1.MultiSelect = false; //不可選多行



   6:            listView1.Scrollable = true; //需要的時候 自動顯示 滾動條



   7:            listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable; //設定點選 標題項目不動作



   8:            listView1.Columns.Add("機種",150,HorizontalAlignment.Left);  //設定標題,寬度,位置



   9:            listView1.Columns.Add("55階",180,HorizontalAlignment.Left);



  10:  



  11:            for (int i = 0; i < 1; i++)



  12:            {



  13:                ListViewItem LM = new ListViewItem(comboBox1.Text.ToString());



  14:  



  15:                LM.SubItems.Add("1");



  16:              



  17:                listView1.Items.AddRange(new ListViewItem[] { LM });



  18:            }




 



排序的部分(CLASS)





   1: class ListViewItemComparer : IComparer



   2: {



   3:     private int col;



   4:     public ListViewItemComparer()



   5:     {



   6:         col = 0;



   7:     }



   8:     public ListViewItemComparer(int column)



   9:     {



  10:         col = column;



  11:     }



  12:     public int Compare(object x, object y)



  13:     {



  14:         return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);



  15:     }



  16: }




用法





   1: //排序 Listiew 4



   2: this.listView4.ListViewItemSorter = new ListViewItemComparer(listView4.Columns[5].Index);



   3: listView4.Sort();