You must Sign In to post a response.
  • Category: ASP.NET

    how to get computer name in asp.net page with c# code

    how to get computer name in asp.net page with c# code

    if i will use my project from server, i want to print the computer name in the corresponding text box.


    if i use
    1).system.environment.machinename
    2).System.Net.Dns.GetHostByName("LocalHost").HostName;

    this above coding is read the username of running place in the server but

    i need to get the current system name if i will run from any system

    how will i write the code


    Thanks and Regards,
    Kugan.T
  • #267561
    The code below retreives the computer name

    Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long

    Public Function GetComputerName() As String
    Dim sResult As String * 255
    GetComputerNameA sResult, 255
    GetComputerName = Left$(sResult, InStr(sResult, Chr$(0)) - 1)
    End Function

  • #267581
    Hello

    You can Use this for your Solution

    This C# code parses the specified connection string to find the host name, instance name, and database name. It returns them in a GroupCollection .

    using System.Text.RegularExpressions;
    ...
    const string CONNECTION_STRING_PATTERN
    = @"=(?<sqlServerName>.+);Initial Catalog=(?<databaseName>\w+);";

    static readonly Regex ConnectionString_Regex
    = new Regex (CONNECTION_STRING_PATTERN, RegexOptions.IgnoreCase);

    static GroupCollection ParseConnectionString (string inputString)
    {
    Match match = ConnectionString_Regex.Match (inputString);
    return match.Groups;
    }

  • #267592
    The code below retreives the computer name

    Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long

    Public Function GetComputerName() As String
    Dim sResult As String * 255
    GetComputerNameA sResult, 255
    GetComputerName = Left$(sResult, InStr(sResult, Chr$(0)) - 1)
    End Function

  • #267661
    The code below retreives the computer name

    Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long

    Public Function GetComputerName() As String
    Dim sResult As String * 255
    GetComputerNameA sResult, 255
    GetComputerName = Left$(sResult, InStr(sResult, Chr$(0)) - 1)
    End Function

  • #267662
    Dim host As System.Net.IPHostEntry
    host = System.Net.Dns.GetHostByAddress(Request.ServerVariables.Item("REMOTE_HOST"))
    strComputerName = host.HostName

  • #267666
    There is a way to get the Client Computer name. Try the following codes.

    <%
    Response.Write(Request.QueryString)
    if Request.QueryString("param")= "yes" then
    Response.write "getComputerName = " & getComputerName()
    end if
    Function getComputerName()
    Dim sIP
    Dim oShell, oExec, sCommand, sOutput
    sIP = Request.ServerVariables("REMOTE_ADDR")
    ''watch for line wrap - begin
    sCommand = "%comspec% /c @echo off & for /f ""tokens=2"" %q in ('ping -n 1 -a " & sIP & "^|find /i ""pinging""') do echo %q"
    ''watch for line wrap - end
    Set oShell = CreateObject("WScript.Shell")
    Set oExec = oShell.Exec(sCommand)
    sOutput = oExec.StdOut.ReadAll
    Set oExec = Nothing
    Set oShell = Nothing
    getComputerName = sOutput
    end Function

    %>


    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/vbscript">

    </script>
    </head>
    <body>
    <p><br>
    This is index.asp page for client computer name test </p>
    <form name="form1" method="post" action="index.asp?param=yes">
    <input type="submit" name="Submit" value="Submit">
    </form>
    <p>  </p>
    </body>
    </html>


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.