[Memo] 整理一下最近寫程式用到的東西

免得以後要找很麻煩。都是 C# 的程式。

Threading 中作 output 到 textbox 中

delegate void SetTextCallback(TextBox tb, string text);

private void SetText(TextBox tb, string text)
{
    if (tb.InvokeRequired)
    {
        SetTextCallback d = new SetTextCallback(SetText);
        this.Invoke(d, new object[] { tb, text });
    }
    else
    {
        tb.Text = text;
    }
}

Extract text in <body> tag

strpage = ""; // Store The HTML Source
strtext = "";

// only fetch text between <body>
ibodystart = strpage.ToLower().IndexOf("<body");
ibodyend = strpage.ToLower().IndexOf("</body>");

if (ibodystart < 0) return;
if (ibodyend < 0) ibodyend = strpage.Length;

// j and k are used to quote text between continous tags
j = strpage.IndexOf(">", ibodystart);

sw = new StreamWriter([FILENAME], false, Encoding.UTF8);

#region filter out html tags, css and scripts, and then just keep plaintext
while (j > 0 && j < ibodyend)
{
    // j and k are used to quote text between continous tags
    k = strpage.IndexOf("<", j);
    
    
    // read text between tags, and store in strtmp
    if (k < 0)
    {
        strtmp = strpage.Substring(j + 1);
    }
    else
    {
        strtmp = strpage.Substring(j + 1, k - j - 1);
    }
    
    
    strtmp = HttpUtility.HtmlDecode(strtmp).Trim();
    
    // concate strtext and strtmp
    if (strtmp != "")
    {
        if (strtext == "")
        {
            sw.WriteLine(strtmp);
            strtext = strtmp;
        }
        else
        {
            sw.WriteLine(" " + strtmp);
        }
    }
    
    // find out next j
    if (k < 0)
    {
        j = -1;
    }
    else
    {
        //check comment
        if (strpage.Substring(k).Length <= 7)
        {
            j = -1;
        }
        else if (strpage.Substring(k, 4) == "<!--")
        {
            j = strpage.IndexOf("-->", k);
            if (j >= 0)
            {
                j = strpage.IndexOf(">", j);
            }
        }
        else if (strpage.ToLower().Substring(k, 7) == "<script")
        {
            j = strpage.ToLower().IndexOf("</script>", k);
            if (j >= 0)
            {
                j = strpage.IndexOf(">", j);
            }
        }
        else if (strpage.ToLower().Substring(k, 6) == "<style")
        {
            j = strpage.ToLower().IndexOf("</style>", k);
            if (j >= 0)
            {
                j = strpage.IndexOf(">", j);
            }
        }
        else
        {
            j = strpage.IndexOf(">", k);
        }
    }
}
#endregion

sw.Close();

Execute the other .exe with parameters from command line (without showing the window). This example uses WordNet.

Process p = new Process();
string strwn1, strwn2;

#region Call wn.exe for wordnet hypernym
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;

// word 1
p.StartInfo.FileName = @"C:\Program Files\WordNet\2.1\bin\wn.exe";
p.StartInfo.Arguments = @"" + w1 + " -hypen"; // w1 is a word

p.Start();

strwn1 = p.StandardOutput.ReadToEnd();

p.WaitForExit();

// word 2
p.StartInfo.FileName = @"C:\Program Files\WordNet\2.1\bin\wn.exe";
p.StartInfo.Arguments = @"" + w2 + " -hypen"; // w2 is the other word

p.Start();

strwn2 = p.StandardOutput.ReadToEnd();

p.WaitForExit();

#endregion

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料