PC Review


Reply
Thread Tools Rate Thread

how to convert this C function into C-Sharp ?

 
 
Ms. Seren
Guest
Posts: n/a
 
      7th Nov 2011
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;
}
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      8th Nov 2011
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


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:39 AM.