安装c#编译环境

安装Microsoft Windows SDK for Windows 7 and .NET Framework 4

假设安装目录为: C:\Windows\Microsoft.NET\Framework64\v4.0.30319

把该目录加入PATH环境变量后,csc.exe可以在命令行直接调用

直接编译

csc /target:exe testshell.cs

引用某个dll进行编译

csc /target:exe /r:somelib.dll testshell.cs

执行shell命令,并输出执行结果

exec shell command, output result to stdout

参考:how-to-execute-command-line-in-c-get-std-out-results

//testshell.cs
using System;
using System.IO;
using System.Diagnostics;

class TestShell {
    public static void Main()
    {

        string[] args = Environment.GetCommandLineArgs();
        string exe = args[1];
        string param = args[2];
        TestShellData x = new TestShellData();
        string data = x.ShellData(exe, param);
        Console.WriteLine(data);
    }
}

class TestShellData
{

    public string ShellData(string exe, string param){
        string some_cmd = '"' + exe + '"' +
                              ' ' +  '"' + param + '"'; 

        Process robot = new Process();
        robot.StartInfo.FileName = "cmd";
        robot.StartInfo.Arguments = "/C \"" + some_cmd + '"';
        robot.StartInfo.UseShellExecute = false;
        robot.StartInfo.RedirectStandardOutput = true;
        robot.Start();    
        
        string data = robot.StandardOutput.ReadToEnd();
        robot.WaitForExit();
        return data;
    }
}

使用例子:

> csc /target:exe testshell.cs
> .\testshell.exe "perl" "-v"

This is perl 5, version 14, subversion 4 (v5.14.4) built for MSWin32-x86-multi-thread

Copyright 1987-2013, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

取网页 get url

见:Get Web Page Contents in Code with C#

using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text; 
using System;

class TestUrl {
    public static void Main()
    {

        string[] args = Environment.GetCommandLineArgs();
        string url = args[1];
        RequestUrl x = new RequestUrl();
        string data = x.GetUrl(url);
        Console.WriteLine(data);
    }
}

class RequestUrl
{

    public string GetUrl(string url){
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
        myRequest.Method = "GET";
        WebResponse myResponse = myRequest.GetResponse();
        StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
        string data = sr.ReadToEnd();
        sr.Close();
        myResponse.Close();
        return data;
    }
}

用Html Agility Pack解析网页

Html Agility Pack 支持通过xpath解析网页dom树,非常好用

using System.Text; 
using System;
using HtmlAgilityPack;

class TestHAP {
    public static void Main()
    {
        string[] args = Environment.GetCommandLineArgs();
        string url = args[1];
        ParseLinkInLi(url);
    }

    static void ParseLinkInLi(string url)
    {
        HtmlWeb web = new HtmlWeb();
        HtmlDocument doc = web.Load(url);

        HtmlNodeCollection li_list = doc.DocumentNode.SelectNodes("//li");
        if(li_list==null){ 
            Console.WriteLine("no link\n");
            return ;
        };

        foreach (HtmlNode li in li_list)
        {
            //注意,xpath以 .// 开始,表示从li的dom开始解析,而不是doc的dom
            HtmlNode link = li.SelectSingleNode(".//a");
            if(link==null){ 
                continue; 
            }

            Console.WriteLine(link.OuterHtml);

            HtmlAttribute name = link.Attributes["name"];
            var n = name.Value;
            Console.WriteLine(n);
        }
    }
}
D:\>csc /target:exe /r:HtmlAgilityPack.dll testhap.cs
Microsoft(R) Visual C# 编译器版本 4.0.30319.17929
用于 Microsoft(R) .NET Framework 4.5
版权所有 (C) Microsoft Corporation。保留所有权利。


D:\>.\testhap.exe http://www.baidu.com
<a href="#" name="ime_hw">手写</a>
ime_hw
<a href="#" name="ime_py">拼音</a>
ime_py
<a href="#" name="ime_cl">关闭</a>
ime_cl

取文本

somenode.InnerText.Trim()

正则

using System.Text.RegularExpressions;

string s = "abc12";

Regex r = new Regex(@"\d");

if(r.IsMatch(s)){
    Console.WriteLine("ok");
}

空字符串

string s;

if(s==String.Empty){
    Console.WriteLine("null");
}

命令行参数

string[] args = Environment.GetCommandLineArgs();

列表,是否为空,join字符串数组

using System.Collections.Generic;

List<string> test_list = new List<string>();
test_list.Add(r);

if (test_list.Count==0) {
    return "";
}else{
    return string.Join(", ", test_list);
}


Published

14 January 2014

Categories

Tags


Share On