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
Solved

C# Get Remote Computer Current User

Posted on 2012-03-20
8
4,279 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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 and create new code templates in NetBeans IDE 8.0 for Windows.

861 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