Solved

C# Get Remote Computer Current User

Posted on 2012-03-20
8
4,397 Views
Last Modified: 2013-12-14
I am trying to run a C# code against a list of live computers and then export the current logged in username in a text file next to the computer name. Can anyone hemo me to figure this out. Please....

Paul
0
Comment
Question by:pauledwardian
[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
  • 3
  • 2
8 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 37745871
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 500 total points
ID: 37746159
0
 

Author Comment

by:pauledwardian
ID: 37747512
That code gave me a lot of unknown variable. Please look at the screenshot in the attachment :

ManagementScope msc = new ManagementScope("\\\\" + IP of the OTHER>computer + "\\root\\cimv2");
 string queryString = "select LogonId from win32_logonsession wherelogontype = 2";
 query = new ManagementObjectSearcher(msc, new SelectQuery(queryString));
 foreach( ManagementObject mo in query.Get())
 {
  UserLogonId = mo["LogonId"].ToString(); // WORKS !!!!!!
 RelatedObjectQuery relatedQuery = new RelatedObjectQuery("associators of {Win32_LogonSession.LogonId='" + mo["LogonId"]+ "'}WHERE AssocClass =  Win32_LoggedOnUser");
 ManagementObjectSearcher searcher = new ManagementObjectSearcher(msc,
relatedQuery);
 foreach (ManagementObject mob in searcher.Get())
  {
  Username = mob["Caption"] + " " + mob["Domain"] + " " +
mob["Name"]; // DOESN'T WORK !!!!!!! they are always empty strings
 }
}
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37747544
Did you try my solutions???
0
 

Accepted Solution

by:
pauledwardian earned 0 total points
ID: 37747625
Also, this code gives me the username on the current omputer regardless of what I put in the machine name:

using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using SetEnv;
 

namespace GetUserInfo
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            string CurrLocUser = string.Empty;
            SetLocEnvironmentVariable UserInfo = new SetLocEnvironmentVariable();
           
            // Create a sample environment variable and set its value (for the current process).
            //SampleSetEnvironmentVariable.SetEnvironmentVariableEx("TESTENV", "TestValue");
 
            CurrLocUser = UserInfo.GetCurrUserInfo("XYZ123"); // System name
 
            // Verify that environment variable is set correctly.
            Console.WriteLine("Current local user: " + CurrLocUser);
 
            return;
 
        }
    }
}
 




GET/SET ENVIRONMENT CODE FILE
-------------------------------
 

using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
 

namespace SetEnv
{
 /// <summary>
 /// Summary description for Class1.
 /// </summary>
 public class SetLocEnvironmentVariable
 {
        // Import the kernel32 dll.
        [DllImport("kernel32.dll",CharSet=CharSet.Auto, SetLastError=true)]
 
        [return:MarshalAs(UnmanagedType.Bool)]
 
        // The declaration is similar to the SDK function
        public static extern bool SetEnvironmentVariable(string lpName, string lpValue);
        public string GetCurrUserInfo(string SystemName)
        {
            string CurrentUser = string.Empty;
 

            CurrentUser = Environment.UserName;
            return CurrentUser;
 
        }
 }
}
0
 

Author Closing Comment

by:pauledwardian
ID: 37923303
Thanks!
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

740 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