On 11/7/2011 2:41 PM, Ms. Seren wrote:
> Hi,
>
> Can any C-Sharp expert out there convert the C function below, into
> C-Sharp code, that I can insert into an already-existing C-Sharp
> program, that will compile? Thanks.
>
> I have an existing C code that compiles in the C environment. I want
> to insert this function into an already running C-Sharp program.
> Because I only have less than a week to get this done, learning C-
> Sharp at this time is not practical.
>
> The C function is simple:
>
> a) get the output of the ipconfig /all command, write it into a tmp
> file
> b) read that file for the string "gateway.net" or for anything string
> that contains the string "192.168.1"
> c) if this is found, set found = 1
> d) when done, delete the tmp file
> e) if found == 0, stop the program
>
> Below is that C function.
>
>
> Check ( )
>
> {
> char str [100];
> int found = 0;
>
> FILE *fi;
>
> system ("ipconfig /all> c:\\temp\\tmp_file");
>
> fi = fopen ("c:\\temp\\tmp_file", "r");
>
> while (! feof (fi))
> {
> fscanf (fi, "%s", str);
>
> if ((strcmp (str, "gateway.net") == 0) || (strstr (str,
> "192.168.1") != NULL))
> found = 1;
> }
>
> fclose (fi);
>
> remove ("c:\\temp\\tmp_file");
>
> if (found == 0)
> exit (0);
>
> return 0;
> }
Two code snippets for inspiration:
using System;
using System.IO;
using System.Diagnostics;
namespace E
{
public class Program
{
public static void Main(string[] args)
{
string fnm = @"c:\work\tmp_file";
Process.Start("cmd", "/c ipconfig/all > " + fnm);
using(StreamReader sr = new StreamReader(fnm))
{
string line;
while((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
File.Delete(fnm);
Console.ReadKey();
}
}
}
using System;
using System.Net.NetworkInformation;
namespace E
{
public class Program
{
public static void Main(string[] args)
{
foreach(NetworkInterface ni in
NetworkInterface.GetAllNetworkInterfaces())
{
Console.WriteLine("name=" + ni.Name);
foreach(UnicastIPAddressInformation addr in
ni.GetIPProperties().UnicastAddresses)
{
Console.WriteLine("addr=" + addr.Address.ToString());
}
foreach(GatewayIPAddressInformation gw in
ni.GetIPProperties().GatewayAddresses)
{
Console.WriteLine("gw=" + gw.Address.ToString());
}
}
Console.ReadKey();
}
}
}
Arne
|