2011年11月14日 星期一

Stop WebClinet.DownloadFiles() in MultiThread

使用WebClinet在multithread環境下下載檔案時的停止方式

ThreadList為 List元件 Thread形態的全域變數

假定Thread宣告內容為
Thread t1 = new Thread(() =>;
{
 try
 {
  using (WebClient mywebclient = new WebClient())
  {
   string sUrlToReadFileFrom = ImageUrl;
   string sFilePathToWriteFileTo = targeturl;
   Stream streamRemote = null;
   Stream streamLocal = null;
   using (WebClient client = new WebClient())
   {
    try
    {
     streamRemote = client.OpenRead(sUrlToReadFileFrom);

     streamLocal = new FileStream(sFilePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None);

     int iByteSize = 0;
     byte[] byteBuffer = new byte[4096];

     while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
     {
      streamLocal.Write(byteBuffer, 0, iByteSize);
     }
    }
    catch (System.Net.WebException){// 可加入console.write}
    catch (Exception){// 可加入console.write}

    if (streamLocal != null){
streamLocal.Close();}
    if (streamRemote != null){streamRemote.Close();}
   }
  }
 }
 catch (System.Net.WebException){// 可加入console.write}
 catch (Exception){// 可加入console.write}
 ThreadList.Add(t1); 

 t1.Start();
}

停止則是另開了一個BackgroundWorker來進行
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
 int icount = 0;
 for (int ia = 0; ia < ThreadList.Count; ia++)
 {
  if (ThreadList[ia].IsAlive)
  {
   ThreadList[ia].Interrupt();
   icount++;
  }
  else
  {
   ThreadList[ia] = null;
  }
 }
 Thread.Sleep(500);
 bool totalStop = false;
 while (!totalStop)
 {
  totalStop = true;
  icount = 0;
  Thread.Sleep(500);
  for (int ia = 0; ia < ThreadList.Count; ia++)
  {
   if (ThreadList[ia] != null)
   {
    if (ThreadList[ia].IsAlive)
    {
     totalStop = false;
     icount++;
    }
    else
    {
     ThreadList[ia] = null;
    }
   }
  }
 }
}


by Keng-li.Lin

2011年11月10日 星期四

giveMeBreakfast 練習程式

最近完成了一個抓網頁的程式

開發環境:
Visual Studio 2010 .net 4

主要功能流程:
1.為抓取洪爺的網頁
2.截取分頁帖數量與連結後,置入Queue中
3.啟動Timer元件進行,對Queue中的連結進行內頁內碼擷取
4.轉換內碼,移除所有特殊html語法,並開啟Thread,使用WebClient進行圖片背景下載
5.將轉換後的內碼組合成html網頁
6.等待圖片下載完成後,自動展示網頁。

主要學習目的為:
web網頁擷取、html語法、多執行序分工

原始碼載點(google Docs空間):
giveMeBreakfast[source].7z
或
https://docs.google.com/leaf?id=0B4zlMKG7kf2mZTMyZDZlM2YtY2NiMi00MzRiLTg4YWQtZDYxMzAwOTFlMDcz

執行檔載點(google Docs空間):
giveMeBreakfast[bin].7z
或
https://docs.google.com/leaf?id=0B4zlMKG7kf2mNzM2NDVlMDMtMmNmMS00ODhiLWI4YTgtNmUxNDRhOTEyODFm

p.s. 此程式行為符合網站操作流程,非惡意攻擊程式,請放心使用。
另外,程式展示的內容含有限制級內容,請慎重考慮後使用。
by Keng-li.Lin

2011年10月31日 星期一

giveMeporm程式練習

最近完成了一個有關抓網頁的練習
有興趣的可以抓下來看

直接開網頁板
giveMeporn[bin].7z

giveMeporn[source].7z

全抓下來在看板
giveMeporn2[bin].7z

giveMeporn2[source].7z

檔案放在Google Docs
可以直接抓不用登入

 p.s.程式內容是18禁限制的
請小孩子不要下載喔

 by Keng-li.Lin

2011年10月27日 星期四

C# 解決WebBrowser 網頁中文為亂碼的方式

WebBrowser WB = new WebBrowser();

using (StreamReader SR = new StreamReader(WB.DocumentStream, System.Text.Encoding.GetEncoding("BIG5"))) // 要先確認該網頁使用的編碼
{
using (StreamWriter SW = new StreamWriter(".\\WebDocument.txt"))
{
SW.Write(SR.ReadToEnd());
SW.Flush();
SW.Close();
}
SR.Close();
}
WB.Dispose();
WB = null;



by Keng-li.Lin

2011年10月17日 星期一

C# 修改資料夾或檔案的隱藏屬性

// 隱藏檔案
String strFileName = @"C:\test.txt";
FileInfo fileInfo = new FileInfo(strFileName);
fileInfo.Attributes = FileAttributes.Hidden;


// 隱藏資料夾
String strDirName = @"C:\test";
DirectoryInfo diMyDir = new DirectoryInfo(strDirName);
diMyDir.Attributes = FileAttributes.Hidden;


Ref:  http://msdn.microsoft.com/zh-tw/library/system.io.filesysteminfo.attributes.aspx

2011年9月13日 星期二

解決重VS2008轉換到VS2010出現 Error: This file requires _WIN32_WINNT to be #defined at least to 0x0403. ....的錯誤


發生原因是代碼定義值是舊版VS訂的
所以只要更換定義值即可

在各專案中,如果標頭檔中有stdafx.h的話
只要在裡面填加

==========================================================================
#ifndef WINVER // 允許使用 Windows 95 與 Windows NT 4 (含) 以後版本的特定功能。
#define WINVER 0x0501 // 將它變更為針對 Windows XP (含) 以後版本適當的值。
#endif

#ifndef _WIN32_WINNT // 允許使用 Windows NT 4 (含) 以後版本的特定功能。
#define _WIN32_WINNT 0x0501 // 將它變更為針對 Windows XP  (含) 以後版本適當的值。
#endif

#ifndef _WIN32_WINDOWS // 允許使用 Windows 98 (含) 以後版本的特定功能。
#define _WIN32_WINDOWS 0x0501 // 將它變更為針對 Windows Me (含) 以後版本適當的值。
#endif

#ifndef _WIN32_IE // 允許使用 IE 4.0 (含) 以後版本的特定功能。
#define _WIN32_IE 0x0601 // 將它變更為針對 IE 5.0 (含) 以後版本適當的值。
#endif
==========================================================================
如果有相同的定義名稱,也可以改定義值即可


by Keng-li.Lin

2011年9月5日 星期一

Windows Azure 建置托管服務

Step.1 開啟Windows Azure 平台 左下角[託管服務、儲存體XXXX]  > 左中間[託管服務] > 左上角[新增託管服務]




Step.2 建立新託管服務 
  1. 設定服務名稱(自取)
  2. 設定URL(想一個全世界唯一個,一般使用英文小寫加數字)
  3. 選擇儲存體(跟Storage同一個比較好)
  4. 選擇不佈署(僅建立服務個體)







Step.3-1 加入憑證 未加入憑證的託管服務無法佈署! 按左上方加入憑證


Step.3-2 加入.pfx加密憑證 加入用來佈署的那台VS所使用的.pfx加密憑證
加密憑證製作方法,請參照



by Keng-li.Lin