夏日部落客BloggerAds《2008‧夏》

2008年4月21日 星期一

拳王爭霸戰

0 意見

 

image

這星期原本打算去士林夜市逛逛,沒想到卻看到百齡高中操場旁,好像有人圍觀,進而一看,沒想到竟然是拳擊比賽,但去的時間似乎已到了比賽的尾聲,不過卻是最精采的重量級比賽,紅色的拳擊手是北體的學生,藍色的拳擊手可能是社會人士,不過在開打時,藍色的呼聲很高,因為身高及手臂的厚實度都佔優勢,不過我知道拳擊其實移動速度是相當重要的,因為小時候是漫畫第一神拳迷,所以誰勝誰負還相當難說,沒想到第一回合就相當精采,紅色選手移動速度優於藍色選手許多,使得藍色選手都攻擊不到要害,反倒是紅色選手卻給了藍色的選手一擊正拳,讓藍色選手直接倒地,站起時又重心不穩,此時場內傳出劇烈的歡呼聲,因為我相信有90%的人都是抱著看精采鏡頭的人吧,反正被打的又不是我的這種心態,不過剛剛的倒地情形讓我已經確定是紅色獲勝,經過3~4回合的激鬥,在結束前的幾秒,發現兩位選手突然有放空,對看著對方的情形,我想是他們應該是很累了,所以才突然放空,最後比數是27比6,紅色獲勝。

這種拳擊比賽在台灣似乎不太盛行,不過看現場的感覺是熱血沸騰阿,縱使比賽選手的程度,比國際級或電視上看的,有相當大的差距,但或許這就是中國人的"溫和"吧,雙方都沒有使出大絕招,可能也怕見血吧,但看免錢的比賽的我,認為算是賺到了,回來之後查了相關新聞,5/25號還有一場比賽,若還是免錢的話我願意去看,因為聲光效果實在是很棒的。

 

相關新聞:

http://news.pchome.com.tw/sport/cna/20080418/index-12085251085405118007.html

2008年4月17日 星期四

C# 複製檔案進度列實作 (Copy file + Progress Bar)

0 意見

應用程式常常會有搬移檔案的動作,但複製檔案時若沒有處裡,很容易造成使用者介面沒回應的畫面,此外若涉及複製大檔案,通常需要顯示進度列,便於使用者知道現在的複製進度,是要繼續下去或者是去取消,這一類的問題,有幾個要注意的問題(1)UI的同步更新問題,(2)非同步複製檔案的實作,我相信這問題已經是老掉牙的問題,但是我在網路上找到的例子大都是這一個使用C#在进度条中显示复制文件的进度 (大部分都是貼這個範例),我大概看過且稍為用了一下(我的開發平台是VS2008),發現有一個問題是他在更新ProgressBar的時候,會有InvalidOperationException[Cross-Thread operation not vaild... 的問題產生,這問題可以上Google稍微查一下就發現問題所在,所以我稍微改寫了一下,畫面及程式碼如下:

Copy

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int totalSize;  //Total Size
        int position;   //Position
        const int BUFFER_SIZE = 4096;
        byte[] buffer;
        FileStream fileWriteStream;
        FileStream fileReadStream;
        public Form1()
        {
            InitializeComponent();
        }

        private void ctrlCopy_Click(object sender, EventArgs e)
        {
            string strFile = "";
            position = 0;
            totalSize = 0;
            OpenFileDialog openDialog = new OpenFileDialog();
            if (openDialog.ShowDialog() == DialogResult.OK)
                strFile = openDialog.FileName;
            else
                return;

            // Reset Status in order to prevent another copying process
            this.totalSize = 0;
            this.position = 0;
            if (this.buffer != null) this.buffer = null;
            if (this.fileWriteStream != null) this.fileWriteStream.Close();
            if (this.fileReadStream != null) this.fileReadStream.Close();

            //Delete file which aready exists.
            if (System.IO.File.Exists("c:\\copyedFile.bin"))
                System.IO.File.Delete("c:\\copyedFile.bin");

            this.fileReadStream = new FileStream(strFile, FileMode.Open, FileAccess.Read);
            this.fileWriteStream = new FileStream("C:\\copyedFile.bin", FileMode.Append, FileAccess.Write);
            totalSize = (int)fileReadStream.Length;
            this.progressBar1.Maximum = totalSize;

            //Copy file while larger than 4KB.
            if (totalSize > BUFFER_SIZE)
            {
                buffer = new byte[BUFFER_SIZE];

                // Async Invoke
                fileReadStream.BeginRead(buffer, 0, BUFFER_SIZE, new AsyncCallback(AsyncCopyFile), null);
            }
            else
            {
                this.fileReadStream.Close();
            }
        }
        // Asynchronously copy file
        private void AsyncCopyFile(IAsyncResult ar)
        {
            int readedLength;

            // Lock FileStream
            lock (this.fileReadStream)
            {
                readedLength = this.fileReadStream.EndRead(ar);   // When stream endread, get readed length
            }

            // Write to disk
            this.fileWriteStream.Write(buffer, 0, buffer.Length);

            // Current stream position
            position += readedLength;

            // Response UI
            MethodInvoker m = new MethodInvoker(SynchProgressBar);
            if (this.progressBar1.InvokeRequired)
            {
                this.progressBar1.Invoke(m);
            }

            if (position >= totalSize) // Read over.
            {
                this.fileWriteStream.Close();        //Close FileStream 
                this.fileReadStream.Close();
                return;
            }

            // Continue to read and write
            lock (this.fileReadStream)
            {
                int leftSize = totalSize - position;

                if (leftSize < BUFFER_SIZE)
                    buffer = new byte[leftSize];

                this.fileReadStream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(AsyncCopyFile), null);
            }
        }

        private void SynchProgressBar()
        {
            this.progressBar1.Value = position;
        }
    }
}

不過細看有一些細節的部份要處裡,如檔案小於4KB時要如何處理,若要將此方法包成物件,要如何用delegate or event傳給上層UI... 這些問題就交給各位看官嚕。 此外有些觀念也是很重要的,在此附上連結: http://www.cnblogs.com/c2303191/articles/826571.html

2008年4月14日 星期一

HtmlTextWriter 實作簡介

0 意見

HtmlTextWriter使用動機

最近剛好在研究html由程式產生,發現在.net framework下有一個實用簡單的類別-HtmlTextWriter,藉由這個類別可以快速的由程式中寫出Html碼,命名空間是 System.Web.UI,如果在WinForm下使用,請引用(Reference)System.Web.dll

HtmlTextWriter與Html32TextWriter
HtmlTextWriter支援Html4.0標準,而Html32TextWriter支援Html3.2標準,在msdn中一般不建議將採用Html32TextWriter,它為支援低版本的瀏覽器而設計的,所以建議採用HtmlTextWriter物件,由名稱上也可猜測這類別比較正統,沒有多餘的贅字。

 

TextWriter - StringWriter, StreamWriter 簡介
在介紹HtmlTextWriter使用前,必須先介紹一些相關的類別,因為HtmlTextWriter本身並不支援輸出的功能,必須由TextWriter的類別來實現輸出的功能。

TextWriter: 表示可以寫入一連串連續字元的寫入器,這個類別是抽象的。

StringWriter: 實作TextWriter以便將資訊寫入字串。資訊儲存在基礎 StringBuilder 中。

StreamWriter: 實作以特定的編碼方式將字元寫入位元組資料流的TextWriter。 

HtmlTextWriter初始化

HtmlTextWriter在建構時必須要接串接TextWriter,本身不提供輸出的方法或屬性。

建構子: public HtmlTextWriter (TextWriter writer)

輸出成檔案

System.IO.StreamWriter streamWriter = new System.IO.StreamWriter("C:\\htmlSample.htm");
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(streamWriter);

streamWriter.toString();

streamWriter.Close();

在C:下可看到htmlSample.htm檔案

輸出成字串:

StringWriter strWriter=new System.IO.StringWriter();
HtmlTextWriter htmlWriter=new HtmlTextWriter(strWriter);

獲得輸出結果 string result =  strWriter.toString();

HtmlTextWriter常用方法

1. void RenderBeginTag(HtmlTextWriterTag tagKey)
    用於Html開始標籤,如<body>,HtmlTextWriterTag是列舉類別,內部有所有Html 4.0的列舉標籤,如果要增加body標籤,就是RenderBeginTag(HtmlTextWriterTag.Body)

2. void RenderEndTag() 
    與RenderBeginTag對應,有幾個RenderBeginTag就必須有幾個RenderEndTag。如下面的例子:
<html>
    <head></head>
    <body></body>
</html>
上面一段是html基本架構,使用HtmlTextWriter生成就是
   htmlWriter.RenderBeginTag(HtmlTextWriterTag.Html);
   htmlWriter.RenderBeginTag(HtmlTextWriterTag.Head);
   htmlWriter.RenderEndTag();   //End of Head
   htmlWriter.RenderBeginTag(HtmlTextWriterTag.Body);
   htmlWriter.RenderEndTag();  //End of Body
   htmlWriter.RenderEndTag();  //End of Html
3. void AddAttribute() 
   用於增加標籤的屬性,例如<img>標籤的url屬性、width屬性等。注意:AddAttribute必須出現在RenderBeginTag的前面

ex:
   htmlWriter.AddAttribute("url","../xxx.gif");
   htmlWriter.AddAttribute("width","50");
   htmlWriter.AddAttribute("height","50");
   htmlWriter.RenderBeginTag(HtmlTextWriterTag.Img);
   htmlWriter.RenderEndTag();

輸出: <img url="../xxx.gif" width="50" height="50" />。

4.void AddStyleAttribute()

    由於Html並不是很嚴謹的語言,所以上面的函式就可實作所須輸出的字串,但建議還是採用以下範例,以下3個例子,所產生的字串都相同,也能正確輸出


(a)
htmlWriter.AddAttribute("style", "margin:0px; color: #ff0; overflow: auto");
(b)
htmlWriter.AddStyleAttribute("margin", "0px");
htmlWriter.AddStyleAttribute("color", "#ff0");
htmlWriter.AddStyleAttribute("overflow", "auto");
(c)
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Margin, "0px");
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Color, ""#ff0");
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Overflow, "auto");
輸出: Style="margin:0px; color: #ff0; overflow: auto"
我採用的是第三種方法,是因為它有提供對應的列舉,不想全部手動自己寫。

5. void Write()和void WriteLine() 
   產生Html標籤以外的所有信息。

此外,若是從WinForm上的顏色轉換到網頁上的顏色,可採用此函式System.Drawing.ColorTranslator.ToHtml(Color c);

實作 Web 跑馬燈

 image

private void SaveMessageToHtml1(string htmPath)
{
    System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(htmPath);
    System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(streamWriter);

    // Html
    htmlWriter.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Html);

    // Head
    htmlWriter.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Head);
    htmlWriter.RenderEndTag(); // End of Head

    // Meta
    htmlWriter.AddAttribute("http-equiv", "content-type");
    htmlWriter.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Content, "text/html; charset=UTF-8");
    htmlWriter.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Meta);
    htmlWriter.RenderEndTag(); // End of Meta

    // Body
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.FontFamily, "Arial");
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Margin, "0px");
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Color, System.Drawing.ColorTranslator.ToHtml(Color.Yellow));
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.BackgroundColor, System.Drawing.ColorTranslator.ToHtml(Color.Black));
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Overflow, "auto");
    htmlWriter.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Body);

    // Marquee
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Position, "relative");

    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Top, "25%");
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Height, "100%");
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.FontSize, string.Format("{0}px", "20"));
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Overflow, "hidden");
    htmlWriter.AddAttribute("behavior", "scroll");
    htmlWriter.AddAttribute("direction", "left");

    htmlWriter.AddAttribute("loop", "-1");
    htmlWriter.AddAttribute("scrollamount", this.scrollOffset.Text);
    htmlWriter.AddAttribute("scrolldelay", this.scrollDelay.Text);
    htmlWriter.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Marquee);
    htmlWriter.Write("Hello! This is Web Brower-Marquee");

    htmlWriter.WriteLine();
    htmlWriter.RenderEndTag(); // End of Marquee
    htmlWriter.RenderEndTag(); // End of Body
    htmlWriter.RenderEndTag(); // End of Html

    streamWriter.ToString();
    streamWriter.Close();
    htmlWriter.Close();
    streamWriter.Dispose();
    htmlWriter.Dispose();

    // 除錯用
    System.Diagnostics.Process.Start("IEXPLORE.EXE", htmPath);
}

Reference:

http://www.cnblogs.com/tonyqus/archive/2005/02/15/104576.html

MSDN

2008年4月8日 星期二

SQL Server 2005 Backup 介紹

0 意見

最近對SQL Server的備份有些需求: 希望能夠定期備份資料庫,且希望備分檔名是XX_yyyyMMdd.bak 的格式,這樣輩分的檔案就會對應備份的日期,不會被抹寫掉,此外還要能夠備分到另一台主機上。

我的平台是SQL 2005,備份的方式其實很簡單,首先先簡易說明SQL提供的備份方式,有下列四種:

SQL Server provides four different methods for backing up your database:

  • A full backup makes a complete backup of your database. You will almost always need to start your backup strategy with a full backup of your database.
  • A file backup is useful when your database is so large that a full backup would take too long.
  • A transaction log backup creates a copy of all changes made to the database that are currently stored in the transaction log.
  • A differential backup stores all changes that have occurred to the database since the last full backup.

我這邊僅介紹第一種full backup,這種方式不適合資料量很大的資料庫,操作方式如下。

image

針對要備份的資料庫,點擊滑鼠右鍵,會跳出選單,選擇紅框部份,進入畫面後,選擇下圖紅框部份

image

接著,就會進入編輯備份的工作,重點在於Steps的選項中,必須新增新工作(實際要備份的工作),

image

這邊可以編輯Script做進階的動作,而我備份的檔名正好需要用到此功能,以下是我備份到本機的方式。這邊有一個小細節要注意,N這個參數代表是本機磁碟的意思,所以一定要有的,此外單引號要注意,不然也會有錯誤。

image

下圖是我備份到網路磁碟的方式。

image

接著設定排程都有介面可以設定,這邊也不再贅述。這樣就簡易的設定可以完成一個簡單的備份工作。

此外,設定完成後,實際執行此工作是由SQL Server Agent完成的,可以觀察到Jobs多了一項剛剛新增的工作,如果想測試剛才的設定是否可以正確執行,可以選擇Start Job就會執行此項目的設定,若有錯誤則可以觀看歷史紀錄檔,也可以以進入屬性進行修改。

image 

參考連結:

http://www.devx.com/getHelpOn/10MinuteSolution/16507/1954

http://blog.sjzj.com.cn/article.asp?id=689

http://www.sqlteam.com/tag/sql-server-backup-restore

2008年4月7日 星期一

DirSync 備份軟體介紹

0 意見

image

先前跟同事買了一台IBM T42 MB,由於在這台筆電上花的時間越來越多,而且思考到如果在此台NB 開發新的套件或模組,一但只要電腦不小心毀損,那裡面的心血結晶,不就附諸流水,所以備份是很重要的,但又不想每次是思考我哪些東西備分過哪些沒有備份過,用"純手動"的方式備份,每次都這樣備份會令人抓狂,所以今天介紹一個備份軟體 DirSync,簡單又實用,而且是不用安裝的綠色軟體,首先,你必須建立job,一個job代表一個備份的事項,你可以針對job設定備份的方式,目前提供的方式有五種,但最常用的是(a)(b),將有新增的部份做備份及更新,ex:從NB備份(source)到PC or USB HD(target),source有的檔案是a b c d 這三個檔案,而d這個檔案從source被刪除,target有a c d的檔案,則選用(a)選項,那備份後target,則會有 a b c d 這四個檔案,可是若選擇(c),則target 只會有a b c 這三個檔案,選項(d)則是可以source - target 互相備份,選項(e)是用來還原,允許較舊的檔案複寫較新的檔案,每個選項下都會有說明,所以並不會很難懂,此外也支援排程的備份功能,若有這需求的人不妨去試試。