0

I am working on ASP.NET 4.0 and Visual Studio 2010.In my Web Project I am running a Command Process to get Physical Address of the System and Coping the result and assigning it to string variable and doing substring of the String variable to get the desired result only.In my localhost it is working correcltly,but when I deploy it I get the System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string. exception.The Code I tried is,

   string command = "getmac";

   Process process = new Process();
   ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/c " + command);
   startInfo.CreateNoWindow = true;
   startInfo.UseShellExecute = false;
   startInfo.RedirectStandardOutput = true;
   process.StartInfo = startInfo;
   process.Start();

   string mac = "";
   string output = process.StandardOutput.ReadToEnd();
   if (!string.IsNullOrEmpty(output))
   {
      mac = output.Substring(162, 20).Trim();
   }
   process.WaitForExit();

  if (!string.IsNullOrEmpty(mac))
  {
    mactxtbox.Text = mac;
  }

Edit

The below is my output Variable

Physical Address Transport Name
=================== ========================================================== 00-00-00-00-00-00 \Device\Tcpip_{00000000-0000-0000-0000-000000000000}

all I want is Just that Physical Address in the format 00-00-00-00-00-00

Rajesh
  • 1,600
  • 5
  • 33
  • 59
  • `mac = output.Substring(162, 20).Trim();` this line cause the exception. You can log `output` variable to a text file and see what inside it – Doan Cuong Oct 09 '13 at 08:56
  • @DoanCuong It is working nice in `localhost` and text is coming but the exception comes When the page is Published in the Web server – Rajesh Oct 09 '13 at 09:01

2 Answers2

0

try adding this:

   if (!string.IsNullOrEmpty(output) && output.Lenght >= (162+20))
   {
      mac = output.Substring(162, 20).Trim();
   }

If you read the exception it tells you exacly whats wrong:

System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string

and tis i because the output varaible doenst have a least 182 characthers

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
0

This is not a good way of getting the MAC address as it is sensitive to the format returned by the getmac command. I would recommend not calling out to getmac and using the technique outlined in the answer to this question

Community
  • 1
  • 1
Ken Keenan
  • 9,818
  • 5
  • 32
  • 49
  • First I tried like I said in this [question](http://stackoverflow.com/questions/19245571/how-to-get-the-client-system-mac-id-and-other-details-like-ip-address-etc) – Rajesh Oct 09 '13 at 09:12