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

本文关键字:字符串 输出 变量 | 更新日期: 2023-09-27 18:34:12

在此代码之后更新为原始帖子 ---此代码是对大卫一直在帮助我完成的更新 抛出一个错误需要帮助

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  ";

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

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

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

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