iLeichun

当前位置:首页C#

C#数组初始化的3种方式

分类:C#  来源:网络  时间:Dec 28, 2017 3:45:55 PM

C#数组的初始化有3种方式,方便在不同的场合选择不同的方式来初始化,下面将举例说明:

(1)一维数组

初始化的3种方式:

①int[] arr = new int[2];

arr[0] = 1;

arr[1] = 2;

②int[] arr = new int[]{1, 2};

③int[] arr = {1, 2};


(2)二维(或多维)数组

本例以二维数组为例,多维数组类推,初始化的3种方式:

①int[,] arr = new int[2, 3];

arr[0, 1] = 1;

arr[0, 2] = 2;

arr[0, 3] = 3;

arr[1, 1] = 11;

arr[1, 2] = 22;

arr[1, 3] = 33;

②int[,] arr = new int[,]{{1, 2, 3}, {11, 22, 33}};

③int[,] arr = {1, 2, 3}, {11, 22, 33};


(3)不规则数组

int[][] arr = new int[2][];

arr[0] = new int[4];

arr[1] = new int[5];

然后要对子数组进行初始化,可使用上述(1)(2)中的3种方式进行,如:

arr[0][0] = 1;

arr[0][1] = 2;

...


C#把图片保存到数据库

分类:C#  来源:网络  时间:Dec 17, 2010 11:27:55 PM

在开发的过程中,难免遇到图片保存问题,解决的方法有很多,这里我把图片以二进制的形式保存到数据库中,也许这个形式并不是最高效的方式,但也不失为一种好的方法吧.呵呵,下面简单的demo可以作为参考:

1Code#region Code
2
3         //单击"浏览"按钮
4
5         private void button1_Click(object sender, System.EventArgs e)
6
7         {
8
9               DialogResult result=this.openFileDialog1.ShowDialog();
10
11              if(result==DialogResult.OK)
12
13              {
14
15                   this.textBox1.Text=this.openFileDialog1.FileName.ToString
16
17();
18
19                    Image img = Bitmap.FromFile(this.textBox1.Text);
20
21                   this.pictureBox1.Image=img;
22
23               }
24
25      
26
27          }
28
29         //单击"确定"按钮
30
31         private void button2_Click(object sender, System.EventArgs e)
32
33         {
34
35              //插入数据库操作,图片类型的参数为PicToBinary()返回的byte[]即可
36
37把图片以字节的形式保存到数据库中
38
39          }
40
41         //图片转换为字节数组
42
43         private byte[] PicToBinary()
44
45         {
46
47              //创建参数集
48
49              string path = this.textBox1.Text.Trim();
50
51              byte[] source = null;
52
53              if(!path.Equals("") && File.Exists(path))
54
55              {
56
57                    FileStream fs=new FileStream
58
59(path,FileMode.Open,FileAccess.Read);//创建文件流
60
61                    source=new byte[(int)fs.Length];
62
63                    fs.Read(source,0,(int)fs.Length);
64
65                    Image img = Bitmap.FromStream(fs);//把文件流转换为图片
66
67                   if(img.Width > 300 || img.Height > 400)
68
69                   {
70
71                        MessageBox.Show("图片过大,请上传400*300以下的图片");
72
73                       return;
74
75                    }                  
76
77                    fs.Flush();
78
79                    fs.Close();
80
81               }
82
83              return source;
84
85          }
86
87
88
89         #endregion1Code#region Code
2
3         //单击"浏览"按钮
4
5         private void button1_Click(object sender, System.EventArgs e)
6
7         {
8
9               DialogResult result=this.openFileDialog1.ShowDialog();
10
11              if(result==DialogResult.OK)
12
13              {
14
15                   this.textBox1.Text=this.openFileDialog1.FileName.ToString
16
17();
18
19                    Image img = Bitmap.FromFile(this.textBox1.Text);
20
21                   this.pictureBox1.Image=img;
22
23               }
24
25      
26
27          }
28
29         //单击"确定"按钮
30
31         private void button2_Click(object sender, System.EventArgs e)
32
33         {
34
35              //插入数据库操作,图片类型的参数为PicToBinary()返回的byte[]即可
36
37把图片以字节的形式保存到数据库中
38
39          }
40
41         //图片转换为字节数组
42
43         private byte[] PicToBinary()
44
45         {
46
47              //创建参数集
48
49              string path = this.textBox1.Text.Trim();
50
51              byte[] source = null;
52
53              if(!path.Equals("") && File.Exists(path))
54
55              {
56
57                    FileStream fs=new FileStream
58
59(path,FileMode.Open,FileAccess.Read);//创建文件流
60
61                    source=new byte[(int)fs.Length];
62
63                    fs.Read(source,0,(int)fs.Length);
64
65                    Image img = Bitmap.FromStream(fs);//把文件流转换为图片
66
67                   if(img.Width > 300 || img.Height > 400)
68
69                   {
70
71                        MessageBox.Show("图片过大,请上传400*300以下的图片");
72
73                       return;
74
75                    }                  
76
77                    fs.Flush();
78
79                    fs.Close();
80
81               }
82
83              return source;
84
85          }
86
87
88
89         #endregion1Code#region Code
2
3         //单击"浏览"按钮
4
5         private void button1_Click(object sender, System.EventArgs e)
6
7         {
8
9               DialogResult result=this.openFileDialog1.ShowDialog();
10
11              if(result==DialogResult.OK)
12
13              {
14
15                   this.textBox1.Text=this.openFileDialog1.FileName.ToString
16
17();
18
19                    Image img = Bitmap.FromFile(this.textBox1.Text);
20
21                   this.pictureBox1.Image=img;
22
23               }
24
25      
26
27          }
28
29         //单击"确定"按钮
30
31         private void button2_Click(object sender, System.EventArgs e)
32
33         {
34
35              //插入数据库操作,图片类型的参数为PicToBinary()返回的byte[]即可
36
37把图片以字节的形式保存到数据库中
38
39          }
40
41         //图片转换为字节数组
42
43         private byte[] PicToBinary()
44
45         {
46
47              //创建参数集
48
49              string path = this.textBox1.Text.Trim();
50
51              byte[] source = null;
52
53              if(!path.Equals("") && File.Exists(path))
54
55              {
56
57                    FileStream fs=new FileStream
58
59(path,FileMode.Open,FileAccess.Read);//创建文件流
60
61                    source=new byte[(int)fs.Length];
62
63                    fs.Read(source,0,(int)fs.Length);
64
65                    Image img = Bitmap.FromStream(fs);//把文件流转换为图片
66
67                   if(img.Width > 300 || img.Height > 400)
68
69                   {
70
71                        MessageBox.Show("图片过大,请上传400*300以下的图片");
72
73                       return;
74
75                    }                  
76
77                    fs.Flush();
78
79                    fs.Close();
80
81               }
82
83              return source;
84
85          }
86
87
88
89         #endregion
 

C#解析HTML

分类:C#  来源:网络  时间:Aug 18, 2010 11:46:14 AM
方法一:
用System.Net.WebClient下载Web Page存到本地文件或者String中,用正则表达式来分析。这个方法可以用在Web Crawler等需要分析很多Web Page的应用中。
估计这也是大家最直接,最容易想到的一个方法。
所有的href都抽取出来:

using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace HttpGet
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            System.Net.WebClient client = new WebClient();
            byte[] page = client.DownloadData("http://www.google.com");
            string content = System.Text.Encoding.UTF8.GetString(page);
            string regex = "href=["¹](http://|./|/)?w+(.w+)*(/w+(.w+)?)*(/|?w*=w*(&w*=w*)*)?["¹]";
            Regex re = new Regex(regex);
            MatchCollection matches = re.Matches(content);

            System.Collections.IEnumerator enu = matches.GetEnumerator();
            while (enu.MoveNext() && enu.Current != null)
            {
                Match match = (Match)(enu.Current);
                Console.Write(match.Value + " ");
            }
        }
    }
}

一些爬虫的HTML解析中也是用的类似的方法。
 
方法二:

利用Winista.Htmlparser.Net 解析Html。这是.NET平台下解析Html的开源代码,网上有源码下载,百度一下就能搜到,这里就不提供了。并且有英文的帮助文档。找不到的留下邮箱。
个人认为这是.net平台下解析html不错的解决方案,基本上能够满足我们对html的解析工作。
自己做了个实例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Lex;
using Winista.Text.HtmlParser.Util;
using Winista.Text.HtmlParser.Tags;
using Winista.Text.HtmlParser.Filters;


namespace HTMLParser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            AddUrl();
        }

        private void btnParser_Click(object sender, EventArgs e)
        {
            #region
获得网页的html
            try
            {

                txtHtmlWhole.Text = "";
                string url = CBUrl.SelectedItem.ToString().Trim();
                System.Net.WebClient aWebClient = new System.Net.WebClient();
                aWebClient.Encoding = System.Text.Encoding.Default;
                string html = aWebClient.DownloadString(url);
                txtHtmlWhole.Text = html;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            #endregion

            #region
分析网页html节点
            Lexer lexer = new Lexer(this.txtHtmlWhole.Text);
            Parser parser = new Parser(lexer);
            NodeList htmlNodes = parser.Parse(null);
            this.treeView1.Nodes.Clear();
            this.treeView1.Nodes.Add("root");
            TreeNode treeRoot = this.treeView1.Nodes[0];
            for (int i = 0; i
< htmlNodes.Count; i++)
            {
                this.RecursionHtmlNode(treeRoot, htmlNodes[i], false);
            }

            #endregion

        }

        private void RecursionHtmlNode(TreeNode treeNode, INode htmlNode, bool siblingRequired)
        {
            if (htmlNode
== null || treeNode == null) return;

            TreeNode current
= treeNode;
           
TreeNode content ;
            //current node
            if (htmlNode is ITag)
            {
                ITag tag
= (htmlNode as ITag);
                if (!tag.IsEndTag())
                {
                    string nodeString
= tag.TagName;
                   
if (tag.Attributes != null && tag.Attributes.Count > 0)
                    {
                        if (tag.Attributes["ID"] != null)
                        {
                            nodeString = nodeString + " { id="" + tag.Attributes["ID"].ToString() + "" }";
                        }
                        if (tag.Attributes["HREF"] != null)
                        {
                            nodeString = nodeString + " { href="" + tag.Attributes["HREF"].ToString() + "" }";
                        }
                    }
                   
                    current = new TreeNode(nodeString);
                    treeNode.Nodes.Add(current);
                }
            }

            //
获取节点间的内容
            if (htmlNode.Children != null && htmlNode.Children.Count > 0)
            {
                this.RecursionHtmlNode(current, htmlNode.FirstChild, true);
                content = new TreeNode(htmlNode.FirstChild.GetText());
                treeNode.Nodes.Add(content);
            }

            //the sibling nodes
            if (siblingRequired)
            {
                INode sibling = htmlNode.NextSibling;
                while (sibling != null)
                {
                    this.RecursionHtmlNode(treeNode, sibling, false);
                    sibling = sibling.NextSibling;
                }
            }
        }
        private void AddUrl()
        {
            CBUrl.Items.Add("http://www.hao123.com");
            CBUrl.Items.Add("http://www.sina.com");
            CBUrl.Items.Add("http://www.heuet.edu.cn");
        } 
    }
}