string[] strArray = http.Split(new char[] { ',' });
bean.url = strArray[1];
lst.Add(bean);
this.SetDataGrid(lst);
this._urlList = lst;读取的文件格式是
1,需要的字符串
2,需要的字符串标红代码部分跟踪获取到的数组是"1""需要的字符串/r/n2" "需要的字符串/r/n3"而lst.Add(bean)
中只获取到
"需要的字符串/r/n2"
这一条数据 帮帮忙吧
bean.url = strArray[1];
lst.Add(bean);
this.SetDataGrid(lst);
this._urlList = lst;读取的文件格式是
1,需要的字符串
2,需要的字符串标红代码部分跟踪获取到的数组是"1""需要的字符串/r/n2" "需要的字符串/r/n3"而lst.Add(bean)
中只获取到
"需要的字符串/r/n2"
这一条数据 帮帮忙吧
foreach (string bean in strArray)
{
lst.Add(bean);
}
string[] strArray = http.Split(new string[] { ",","/r/n" },StringSplitOptions.None);
System.Text.RegularExpressions.Regex.Split(http, @"/r/n\d,");
很好很强大
中只获取到
"需要的字符串/r/n2"
这一条数据
5555各位大侠忽略了这个原本是while (xxx.Peek() >= 0)本地打开文件命令可以读取下一个字符而不使用他~~~~
string 我找不到方法搞定先谢谢wuyi8808 至少能循环了,你的两次回帖相当牛逼~~~啊哦 可是 我的lst.Add(bean)只能读到第一条数据,后面的就读取不到了
urlBean bean = new urlBean();
string[] strArray = System.Text.RegularExpressions.Regex.Split(http,"\r\n");
foreach (string i in strArray)
{
//this.dataGridView1["url", strArray.Length].Value = strArray.ToString();
bean.url = strArray[0].ToString();
//this.tishikuang.AppendText(strArray[0].ToString() + "\r\n");
lst.Add(bean);
this.SetDataGrid(lst);
this._urlList = lst;
}
this.tishikuang.AppendText("数据读取成功!\r\n");数据为
1234567
7654321
0000000
时
循环三次 读取出了第一条数据………………
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using mshtml;
using System.Collections;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Media;
using System.Runtime.CompilerServices;
using System.Threading;
namespace 读取网络文本文件
{
public partial class chuangkou1 : Form
{
private ArrayList _urlList;
public chuangkou1()
{
InitializeComponent();
}
//开始读取网络文本信息
private void duquxinxi_Click(object sender, EventArgs e)
{
string http = HttpUtil.GetHttp("http://127.0.0.1/test.txt", null);
string duurlpd = "1";
if (http.IndexOf(duurlpd) == -1)
{
this.tishikuang.AppendText("对不起,文本文件配置错误!");
return;
} ArrayList lst = new ArrayList();
urlBean bean = new urlBean();
string[] strArray = System.Text.RegularExpressions.Regex.Split(http,"\r\n");
foreach (string i in strArray)
{
//this.dataGridView1["url", strArray.Length].Value = strArray.ToString();
bean.url = strArray[0].ToString();
//this.tishikuang.AppendText(strArray[0].ToString() + "\r\n");
lst.Add(bean);
this.SetDataGrid(lst);
this._urlList = lst;
}
this.tishikuang.AppendText("数据读取成功!\r\n");
}
private void SetDataGrid(ArrayList lst)
{
this._urlList = lst;
this.dataGridView1.Rows.Clear();
int num = -1;
int num3 = lst.Count - 1;
for (int i = 0; i <= num3; i++)
{
this.dataGridView1.Rows.Add();
urlBean bean = (urlBean)lst[i];
this.dataGridView1["index", i].Value = i + 1;
this.dataGridView1["url", i].Value = bean.url;
}
}
//*/
}
public class AdUrlBean
{
public string newUrl = "";
public string oldUrl = "";
}
public class urlBean
{
// public string id = "";
public string url = "";
}
public class HttpUtil
{
public static string _checkCode = null;
private static CookieCollection _cookieCheckCode = new CookieCollection();
private static CookieCollection _cookieCollection = new CookieCollection();
public static string _password = null;
public static string _userId = null; public static void Clear()
{
_cookieCheckCode = new CookieCollection();
_cookieCollection = new CookieCollection();
_userId = null;
_password = null;
_checkCode = null;
} public static string GetHttp(string url, [Optional, DefaultParameterValue(null)] Encoding eccode)
{
string str;
Stream responseStream = null;
HttpWebRequest request = null;
StreamReader reader = null;
if (eccode == null)
{
eccode = Encoding.UTF8;
}
CookieContainer container = new CookieContainer();
try
{
container.Add(_cookieCollection);
request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = container;
request.Method = "GET";
request.Accept = "*/*";
request.Timeout = 0x4e20;
request.UserAgent = "Mozilla/4.0";
responseStream = ((HttpWebResponse)request.GetResponse()).GetResponseStream();
if (responseStream != null)
{
reader = new StreamReader(responseStream, eccode);
return reader.ReadToEnd();
}
str = "";
}
catch (Exception exception1)
{
//ProjectData.SetProjectError(exception1);
Console.WriteLine(url + ":" + exception1.ToString());
str = "";
// ProjectData.ClearProjectError();
}
return str;
}
}
}
7654321
0000000这既是我的实际文本原本想用,分割,但看了一些人的回复 发现可以直接用回车分割所以改了一点但是这样dataGridView1的url 只能循环输出第一条也就是 1234567
bean.url = strArray[0].ToString(); 把0改成i
看错了..
这里应该是:
foreach (string s in strArray) //把i改成s把,看着顺眼一些.
{ bean.url = s;
lst.Add(bean);
this.SetDataGrid(lst);
this._urlList = lst;
}