Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I get remote Machine Name where Console Application resides when it executes?

Posted on 2012-03-26
11
Medium Priority
?
551 Views
Last Modified: 2012-04-02
I am working on an intranet and have 3 different machines: Development, Test, Production. My Development machine is my laptop; the Test machine is a remote server; the Production machine is a different remote server. I built a simple Console Application using C#, and I would like for it to show me the Machine's name (or domain name) where the executable is actually residing when it is run. It may be running from my laptop machine, but I want it to show me the name of the machine where it resides.

Currently I have tried both of the following, but they both give me my laptop's machine name:

using System.Net;             //For Dns

            string machineName = Environment.MachineName.ToString();
            string strHostName = Dns.GetHostName();

What can I do or use to get the machine name (or host name) of the machine where the Console Application resides when it executes?
0
Comment
Question by:dwoolley3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
11 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37769109
This gives you dev machine name when you run it on server? Strange. Are the names of two machines different? You are not storing the name anywhere are you?
0
 

Author Comment

by:dwoolley3
ID: 37769514
Both commands give me the Dev machine name when I use my laptop to navigate to the Test server (via Windows Mappings) and execute the Console Application executable that resides on the Test server.  From one perspective, I suppose it is executing on my laptop. Is there a way to have it show that the executable actually resides on the Test machine?
0
 
LVL 28

Assisted Solution

by:Ark
Ark earned 750 total points
ID: 37770563
When you run executable from remote machine, actually it copies into your machine memory and execute there. You can use Application.StartupPath to get a folder where *.exe file located and then parse it. For local machine you'll get ":" (drive name separator) as a second char
If Application.StartupPath.Substring(1,1)=":" then
   mName=Environment.MachineName
Else
'Here you'll get share name
End If
0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

Author Comment

by:dwoolley3
ID: 37771195
Nice thought Ark, yet it seems that Application.StartUp is only available for Windows Applications (as opposed to Console Applications). I tried to add the namespace System.Windows.Forms, but VS 2008 says that the namespace Windows does not exist in the namespace System.

Perhaps the answer to my question is that I can't get the info that I want when I run the executable remotely from my laptop, since it seems to copy the executable into machine memory and executes it from there (as mentioned by Ark).
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 750 total points
ID: 37772259
I assumed that you are running the exe on the server when you said it resides on the server.

Try

System.AppDomain.CurrentDomain.BaseDirectory

to get the startup path.
0
 

Author Comment

by:dwoolley3
ID: 37772361
Thanks, though the System.AppDomain.CurrentDomain.BaseDirectory is not what I need.

I am able to accomplish my goal of determining whether the machine is production or not by using either

string machineName = Environment.MachineName.ToString();
string strHostName = Dns.GetHostName();

The only catch is that it will show the machine Name that is running the executable and not the machine that is storing the executable. In my case, I can live with this because the production run will be scheduled through SQL Server's Job scheduler that runs on the same production machine. Thus, in production mode, the job will run on the production machine and the program will detect the correct machineName.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37774084
That was a known part I thought.
0
 
LVL 28

Expert Comment

by:Ark
ID: 37774262
I tried to add the namespace System.Windows.Forms
Winform app add reference to windows.forms automatically. To use this namespace in console app, add reference either from .Net components or directly to system.windows.forms.dll
0
 

Author Comment

by:dwoolley3
ID: 37781788
Thank you both for your suggestions, both of which I tested. I added the reference to system.windows.forms.dll -- thank you for that technique.

            string startupPath = Application.StartupPath;
            string baseDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
            Console.WriteLine("StarupPath = " + startupPath);
            Console.WriteLine("BaseDirectory = " + baseDirectory);

With the above code, my executable will produce a complete path only on Development machine:
on Dev machine: C:\Inetpub\wwwroot\etc.
on Test machine: O:\
on Prod machine: A:\

The Test and Prod machine return 3 characters indicating the drive that I have assigned the mapping to from my local machine. If my partner runs these executables from his machine, the drives will be different based on which drives he mapped to the test machine and production machine. We are not logging onto these machines remotely but we are mapping to them through Windows Explorer. Although I would like to have the program differentiate these three environments and thus access things specific to those three environments (like a different database), I am willing to settle for the program differentiating between this kind of a run and the real production run that gets executed on the production machine via SQL Server Job Scheduler.
0
 
LVL 28

Expert Comment

by:Ark
ID: 37784643
  <DllImport("mpr.dll")> _
    Shared Function WNetGetConnection(<MarshalAs(UnmanagedType.LPTStr)> _
                      ByVal localName As String, ByVal remoteName As System.Text.StringBuilder, _
                      ByRef length As Integer) As Integer
    End Function
    Private Function pathToUnc(ByVal path As String)
        Dim length As Integer = 255
        Dim UNC As New System.Text.StringBuilder(length)
        WNetGetConnection(path.Substring(0, 2), UNC, length)
        Return UNC.ToString
    End Function
    
    Private Sub Test()
        Dim dr As String = "O:\"
        MsgBox("The UNC-Path of drive " & dr & " is: " & strToUnc(dr))
    End Sub

Open in new window

0
 
LVL 28

Expert Comment

by:Ark
ID: 37784680
Oops, sorry, seems u'r using CSharp:
DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern int WNetGetConnection(
            [MarshalAs(UnmanagedType.LPTStr)] string localName,
            [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName,
            ref int length);

        public static string GetUNCPath(string originalPath)
        {
            StringBuilder sb = new StringBuilder(512);
            int size = sb.Capacity;
            // look for the {LETTER}: combination ...
            if (originalPath.Length > 2 && originalPath[1] == ':')
            {
                // don't use char.IsLetter here - as that can be misleading
                // the only valid drive letters are a-z && A-Z.
                char c = originalPath[0];
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
                {
                    int error = WNetGetConnection(originalPath.Substring(0, 2),
                        sb, ref size);
                    if (error == 0)
                    {
                        DirectoryInfo dir = new DirectoryInfo(originalPath);
                        string path = Path.GetFullPath(originalPath)
                            .Substring(Path.GetPathRoot(originalPath).Length);
                        return Path.Combine(sb.ToString().TrimEnd(), path);
                    }
                }
            }

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question