문제

UPDATE TO POST ORIGIANAL POST AFTER THIS CODE --- this code is an update to what david has been helping me do its throwing one error need help

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

I have this c# script

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);
    }
}
}

how can I change this so it outputs the string to a variable say "VAR2" and use it in this script

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);
    }
}
}

SO HOW CAN I ADD THE MACHINE NAME SCRIPT TO THIS FUNCTION AND THEN USE IT AS VAR2 any help would be brilliant

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

her is the update david I would like to incude this script in my other script so it looks like this

formData["var1"] = formData["var1"] = string.Format("MachineName: {0}", System.Environment.MachineName);
formData["var2"] = "this is where this script needs to be  ";
도움이 되었습니까?

해결책

Why does it need to be in a separate application? If one application's output is going to be the command-line argument for another application's input, then they're both running on the same machine. Which means, in this case, they'd both get the same value from System.Environment.MachineName.

You can just get the value in the application where it's needed:

formData["var1"] = string.Format("MachineName: {0}", System.Environment.MachineName);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top