显示列表

using System.Runtime.InteropServices;

[DllImport("wininet.dll", SetLastError = true)]
public static extern bool InternetGetCookie(string url, string cookieName, StringBuilder cookieData, ref int size);

private void button5_Click(object sender, EventArgs e)
{
    string url = "http://www.huanghengxu.com"; // 指定相应网站的地址
    string cookieName = null; // 不知道干什么用
    int size = 5000;
    StringBuilder sb = new StringBuilder(size);  // 要指定容量,大于Cookie就可以,否则读取不全

    InternetGetCookie(url, cookieName, sb, ref size);

    System.Windows.Forms.MessageBox.Show(sb.ToString() + "\n" + size.ToString());
}

查看全文 | 分类(C#/CSharp) | 访问(156) | 编辑

可以通过引入 mshtml 中的一些类可以实现操作页面中的元素。
项目引用“C:\WINDOWS\system32\mshtml.tlb”。

mshtml.IHTMLDocument2 iHTMLDocument2 = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
mshtml.IHTMLWindow2 iHTMLWindow2 = (mshtml.IHTMLWindow2)iHTMLDocument2.parentWindow;

// 根据 name 查找页面中的元素。根据类型不同,转成相应的类型。
// 找页面中的 Frame 元素。
mshtml.IHTMLFrameBase2 iFrame = (mshtml.IHTMLFrameBase2)iHTMLDocument2.all.item("FrameName", 0);
mshtml.IHTMLDocument2 iHTMLDocument2 = iFrame.contentWindow.document;



// 找页面中的 Input 元素。
mshtml.IHTMLInputElement input = (mshtml.IHTMLInputElement)iHTMLDocument2.all.item("InputName", 0);

// 找页面中的 Img 元素。
mshtml.IHTMLImgElement img1 = (mshtml.IHTMLImgElement)iHTMLDocument2.all.item("img1", 0);
操作如:img1.src、img1.alt、img1.height + img1.align

mshtml名字空间里对 Html 的每一个元素都有对应的一个类。这个类就可以设置或读取元素的属性。
HTMLDivElement、HTMLSpanElement、HTMLAnchorElement 等等。

// 实现单击按钮
mshtml.IHTMLElement iHTMLElement = (mshtml.IHTMLElement)iHTMLDocument2.all.item(name, 0);
iHTMLElement.click();// 单击

标签:WebBrowser 
查看全文 | 分类(C#/CSharp) | 访问(446) | 编辑

Transfer-Encoding=chunked
是一个大问题。

查看全文 | 分类(C#/CSharp) | 访问(42) | 编辑

意图
专门定义一个类来负责创建其它类的实例,被创建的实例通常都具有共同的父类。
简单工厂模式不属于23个GOF模式。

简单工厂模式中涉及到的角色
1) 抽象产品角色:定义具体产品角色要实现的接口。可以用一个接口或者抽象类实现。
2) 具体产品角色:实现抽象产品角色所定义的接口。
3) 工厂角色:该角色是简单工厂模式的核心。通常根据参数创建一个具体产品的实例。

一个例子
在程序中,要用到一个车的类,就创建一个实例,但当车很多变时,会不断的有新的车出现,那么每次都要增加一个新的车类,还要修改客户程序。使用简单工厂模式可以减少修改的地方。


using System;


namespace Pattern.SimpleFactory
{
    public class Test
    {
        public static void Main()
        {
            Car car;

            Label_001:
            Console.Write("请输入车名:");
            string carType = Console.ReadLine();
            
            car = CarFactory.GetCar(carType); // 返回车的实例。
            if (car == null)
            {
             Console.Write("可能没有这辆车。");
                Console.WriteLine();
                goto Label_001;
            }

            car.Start();
            car.Run();
            car.Stop();
            Console.WriteLine();

            goto Label_001;
            //Console.ReadKey();
        }
    }

    // 1) 抽象产品角色:定义具体产品角色要实现的接口。可以用一个接口或者抽象类实现。
    public abstract class Car
    {
        public abstract void Start();
        public abstract void Run();
        public abstract void Stop();
    }

    // 2) 具体产品角色:实现抽象产品角色所定义的接口。
    public class Benz : Car
    {
        public override void Start()
        {
            Console.WriteLine("The Benz Car Start.");
        }

        public override void Run()
        {
            Console.WriteLine("The Benz Car Running.");
        }

        public override void Stop()
        {
            Console.WriteLine("The Benz Car Stop.");
        }
    }

    public class BWM : Car
    {
        public override void Start()
        {
            Console.WriteLine("The BWM Car Start.");
        }

        public override void Run()
        {
            Console.WriteLine("The BWM Car Running.");
        }

        public override void Stop()
        {
            Console.WriteLine("The BWM Car Stop.");
        }
    }

    // 3) 工厂角色:该角色是简单工厂模式的核心。通常根据参数创建一个具体产品的实例。
    public class CarFactory
    {
        public static Car GetCar(string carType)
        {
            if (carType == "Benz")
            {
                return new Benz();
            }
            else if (carType == "BWM")
            {
                return new BWM();
            }
            else
            {
                return null;
            }
        }
    }
}

使用反射

using System;


namespace Pattern.SimpleFactory
{
    public class Test
    {
        public static void Main()
        {
            Car car;

            Label_001:
            Console.Write("请输入车名:");
            string carType = Console.ReadLine();

            car = CarFactory.GetCar(carType); // 返回车的实例。
            if (car == null)
            {
                Console.Write("可能没有这辆车。");
                Console.WriteLine();
                goto Label_001;
            }

            car.Start();
            car.Run();
            car.Stop();
            Console.WriteLine();

            goto Label_001;
        }
    }
}

using System;
using System.Reflection;


namespace Pattern.SimpleFactory
{
    public abstract class Car
    {
        public abstract void Start();
        public abstract void Run();
        public abstract void Stop();
    }

     public class Benz : Car
    {
        public override void Start()
        {
            Console.WriteLine("The Benz Car Start.");
        }

        public override void Run()
        {
            Console.WriteLine("The Benz Car Running.");
        }

        public override void Stop()
        {
            Console.WriteLine("The Benz Car Stop.");
        }
    }

    public class BWM : Car
    {
        public override void Start()
        {
            Console.WriteLine("The BWM Car Start.");
        }

        public override void Run()
        {
            Console.WriteLine("The BWM Car Running.");
        }

        public override void Stop()
        {
            Console.WriteLine("The BWM Car Stop.");
        }
    }

    public class CarFactory
    {
        public static Car GetCar(string carType)
        {
            Car car;
            try
            {
                // 根据参数确定类名,再创建类的实例,要指定,类所在的.dll文件名和完整的类名(含名字空间)
                car = (Car)Assembly.Load("DLL").CreateInstance("Pattern.SimpleFactory." + carType);
            }
            catch
            {
                car = null;
            }

            return car;
        }
    }
}


总结(2008-6-13)
如果具体产品角色要增加就修改工厂角色类。不过通 Assembly 类可以不修改工厂角色类。

标签:设计模式 
查看全文 | 分类(C#/CSharp) | 访问(24) | 编辑

private void buttonOpenAccess_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.InitialDirectory = Application.ExecutablePath; // 默认打开的目录
    openFileDialog.Filter = "Access Files (*.mdb)|*.mdb|All Files (*.*)|*.*"; // 对话框中显示的文件类型

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        textBoxAccess.Text = openFileDialog.FileName;
    }
}

查看全文 | 分类(C#/CSharp) | 访问(80) | 编辑

意图
为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

就拿坦克来说,坦克有很多个部分组成,比如:轮子、引擎、外壳等等。把每一部分都用一个类表示,坦克的每种操作都需要各个部分共同工作。这样就可以把坦克看成是一个复杂的系统,而里的部件类就是它的子系统。在客户程序中只要调用坦克的操作就可以实现我们要的功能,很好的把复杂的东西包装起来,也减少了客户程序对内部子类的耦合。这样当复杂内部发生变化而不用修改客户程序。

Facade模式的优点
隐藏子系统的复杂性,简单化客户程序的使用。

internal class Wheel()
{
    // 定义轮子的方法和属性等。
}

internal class Engine()
{
    // 定义引擎的方法和属性等。
}

public class TankFacade
{
    Wheel wheel = new Wheel();
    Engine engine = new Engine();

    public void Start()
    {
        // 坦克启动时,轮子、引擎要作些什么。
    }

    public void Stop()
    {
        // 坦克停止时,轮子、引擎要作些什么。
    }

    public void Run()
    {
        // 坦克跑动时,轮子、引擎要作些什么。
    }

    public void Shot()
    {
        // 坦克射击时,轮子、引擎要作些什么。
    }
}

查看全文 | 分类(C#/CSharp) | 访问(42) | 编辑

用于处理多变的情况。

查看全文 | 分类(C#/CSharp) | 访问(31) | 编辑
 过滤HTML标签2008-05-26

把HTML文件中的标签全部过滤。相关的正则表达式,整理出5条,顺序不要乱,有待验证。
// 过滤 <!--  -->
string pattern1 = @"<!--[\s\S]*?-->";
// 过滤 <script>...</script>
string pattern2 = @"<[\s]*?script[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?script[\s]*?>";
// 过滤 <style>...</style>
string pattern3 = @"<[\s]*?style[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?style[\s]*?>";
// 过滤事作 匹配如:<img onclick="if(this.width>1000) return false;"/>
string pattern4 = @"<[^>]*?([^>]*?[\s]on[\w]+[\s]*?=[\s]*?([""']?)([^\2]+?)\2)+[^>]*?>";
// 过滤HTML标签
string pattern5 = @"<[\s\S]*?>";

查看全文 | 分类(C#/CSharp) | 访问(71) | 编辑

用Webbrowser控件加载网页时,通常会用DocumentCompleted事件来指示网页加载完毕。但当加载的网页包含frame时,可能会多次触发该事件,所以不能简单地通过它来判断网页加载完毕。要通过WebBrowserNavigatedEventArgs来判断。



private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (e.Url != webBrowser1.Document.Url) return;
 
    // 当 e.Url == webBrowser1.Document.Url 我们要的网页加载完毕
    // 加载完毕
    // ...
}

标签:Webbrowser 
查看全文 | 分类(C#/CSharp) | 访问(610) | 编辑
 DataTable排序2008-05-22

DataTable dt = dataTable.Clone();
dt.Rows.Clear();

DataRow[] rows = dataTable.Select(String.Empty, "ArticleID Desc");
foreach (DataRow row in rows)
{
    dt.ImportRow(row); //数据导入新的DataTable中
}

查看全文 | 分类(C#/CSharp) | 访问(721) | 编辑
 总数:40   页次:1/4   首 页   上一页   下一页   尾 页