c#更改此行,以便将字符串作为变量输出

时间:2013-10-06 19:49:25

标签: c# linq variables postdata

在此代码之后更新发布原始帖子---此代码是对david帮助我做出错误的更新需要帮助

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Net;
using System.IO;


namespace ConsoleApplication1
{
    class Program
    {
    static void Main(string[] args)
    {
        string URL = "http://localhost/test2.php";
        WebClient webClient = new WebClient();

        NameValueCollection formData = new NameValueCollection();
        formData["var1"] = formData["var1"] = string.Format("MachineName: {0}", System.Environment.MachineName);
        formData["var2"] = ip();


        byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
        string responsefromserver = Encoding.UTF8.GetString(responseBytes);
        Console.WriteLine(responsefromserver);
        webClient.Dispose();
        System.Threading.Thread.Sleep(5000);
    }
    public void ip()
    {
        String publicIP = "";

        System.Net.WebRequest request = System.Net.WebRequest.Create("http://checkip.dyndns.org/");
        using (System.Net.WebResponse response = request.GetResponse())
        {
            using (System.IO.StreamReader stream = new System.IO.StreamReader(response.GetResponseStream()))
            {
                publicIP = stream.ReadToEnd();
            }
        }

        //Search for the ip in the html
        int first = publicIP.IndexOf("Address: ") + 9;
        int last = publicIP.LastIndexOf("</body>");
        publicIP = publicIP.Substring(first, last - first);

        Console.WriteLine(publicIP);
        System.Threading.Thread.Sleep(5000);


    }
}
}

this is the error I am getting 
Error   2 - An object reference is required for the non-static field, method, or property 'ConsoleApplication1.Program.ip()'
I am trying to create a function that will send the out put as var2

我有这个c#脚本

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication4
{
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("MachineName: {0}", System.Environment.MachineName);
        System.Threading.Thread.Sleep(5000);
    }
}
}

如何更改此值,以便将字符串输出到变量“VAR2”并在此脚本中使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Net;
using System.IO;


namespace ConsoleApplication1
{
class Program
{

    static void Main(string[] args)
    {
        string URL = "http://localhost/test2.php";
        WebClient webClient = new WebClient();
        NameValueCollection formData = new NameValueCollection();
        formData["var1"] = "THIS IS WHERE VAR2 NEEDS TO BE ";


        byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
        string responsefromserver = Encoding.UTF8.GetString(responseBytes);
        Console.WriteLine(responsefromserver);
        webClient.Dispose();
        System.Threading.Thread.Sleep(5000);
    }
}
}

我如何将机器名称脚本添加到此功能中,然后将其用作VAR2 任何帮助都会很棒

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication5
{
class Program
{
    public static int Main(string[] args)
    {
        String publicIP = "";

        System.Net.WebRequest request = System.Net.WebRequest.Create("http://checkip.dyndns.org/");
        using (System.Net.WebResponse response = request.GetResponse())
        {
            using (System.IO.StreamReader stream = new System.IO.StreamReader(response.GetResponseStream()))
            {
                publicIP = stream.ReadToEnd();
            }
        }

        //Search for the ip in the html
        int first = publicIP.IndexOf("Address: ") + 9;
        int last = publicIP.LastIndexOf("</body>");
        publicIP = publicIP.Substring(first, last - first);

        Console.WriteLine(publicIP);
        System.Threading.Thread.Sleep(5000);
        return 0;
    }
}
}

她是更新大卫我想在我的其他脚本中包含这个脚本所以它看起来像这样

formData["var1"] = formData["var1"] = string.Format("MachineName: {0}", System.Environment.MachineName);
formData["var2"] = "this is where this script needs to be  ";

1 个答案:

答案 0 :(得分:1)

为什么需要在单独的应用程序中?如果一个应用程序的输出将成为另一个应用程序输入的命令行参数,那么它们都在同一台机器上运行。这意味着,在这种情况下,他们都会从System.Environment.MachineName得到相同的值。

您可以在应用程序中获取所需的值:

formData["var1"] = string.Format("MachineName: {0}", System.Environment.MachineName);