Solved

C# Get Remote Computer Current User

Posted on 2012-03-20
8
4,029 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
Comment Utility
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 500 total points
Comment Utility
0
 

Author Comment

by:pauledwardian
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
Did you try my solutions???
0
 

Accepted Solution

by:
pauledwardian earned 0 total points
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now