我是C#的新手,我真的需要知道如何从另一个方法调用/使用字符串.
例如:
public void button1_Click(object sender, EventArgs e)
{
string a = "help";
}
public void button2_Click(object sender, EventArgs e)
{
//this is where I need to call the string "a" value from button1_click
string b = "I need ";
string c = b + a;
}
Run Code Online (Sandbox Code Playgroud)
所以在这个例子中我需要调用函数中函数中定义的字符串" a "button1_Click()button2_Click()
谢谢!!
我正在尝试为matchcollection创建一个Parallel.Foreach循环.这是我建造的刮刀.我只需要知道在Parallel.Foreach中放什么
MatchCollection m = Regex.Matches(htmlcon, matchlink, RegexOptions.Singleline);
Parallel.ForEach(WHAT DO I PUT HERE? =>
{
Get(match.Groups[1].Value, false);
Match fname = Regex.Match(htmlcon, @"<span class=""given-name"(.*?)</span>", RegexOptions.Singleline);
Match lname = Regex.Match(htmlcon, @"span class=""family-name"">(.*?)</span>", RegexOptions.Singleline);
firstname = fname.Groups[1].Value;
lastname = lname.Groups[1].Value;
sw.WriteLine(firstname + "," + lastname);
sw.Flush();
}):
Run Code Online (Sandbox Code Playgroud)
我试过了:
Parallel.ForEach<MatchCollection>(m,match =>
Run Code Online (Sandbox Code Playgroud)
但没有运气!
提前致谢!:)
我是C#的一个真正的小伙子试图根据我的一个朋友在他的一个应用程序中使用的短代码编写一个小的XML替代程序.
我遇到这条线路有问题:
StreamReader sr = new StreamReader(textBox1.Text);
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:"空路径名称不合法." 为什么这不起作用?
代码是:
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 System.IO;
namespace ReplaceMe
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
StreamReader sr = new StreamReader(textBox1.Text);
StreamWriter sw = new StreamWriter(textBox1.Text.Replace(".", "_new."));
string cur = "";
do
{
cur = sr.ReadLine();
cur = cur.Replace(textBox2.Text, textBox3.Text);
sw.WriteLine(cur);
}
while (!sr.EndOfStream);
sw.Close();
sr.Close();
MessageBox.Show("Finished, the new file is in the same …Run Code Online (Sandbox Code Playgroud) 我正在开发一款可在Google搜索结果网址中搜索电子邮件地址的应用.问题是它需要将每个页面中找到的值+它找到电子邮件的URL返回到包含2列的数据网格视图:电子邮件和URL.我正在使用Parallel.ForEach这个,但它当然返回随机URL,而不是它真正找到的电子邮件.
public static string htmlcon; //htmlsource
public static List<string> emailList = new List<string>();
public static string Get(string url, bool proxy)
{
htmlcon = "";
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
if (proxy)
req.Proxy = new WebProxy(proxyIP + ":" + proxyPort);
req.Method = "GET";
req.UserAgent = Settings1.Default.UserAgent;
if (Settings1.Default.EnableCookies == true)
{
CookieContainer cont = new CookieContainer();
req.CookieContainer = cont;
}
WebResponse resp = req.GetResponse();
StreamReader SR = new StreamReader(resp.GetResponseStream());
htmlcon = SR.ReadToEnd();
Thread.Sleep(400);
resp.Close();
SR.Close();
}
catch (Exception)
{
Thread.Sleep(500); …Run Code Online (Sandbox Code Playgroud)