RT:
.net中怎样调用当前机器的用户名,不是带有domain的用户名
比如你在cmd中键入 set user 后显示的那个username是怎么调用的
而不是用identity调用处 domain\username

解决方案 »

  1.   

    Environment.UserName
    当前及其用户名~~[align=center]********************************************************
    本内容用 CSDN小秘书 回复
    每天回帖即可获得10分可用分!
    ********************************************************[/align]
      

  2.   


    System.Environment.MachineName //当前电脑名
    System.Environment.UserDomainName //当前电脑所属网域
    System.Environment.UserName //当前电脑用户
      

  3.   

    C# 取得当前系统所有用户名
    using System;
    using System.Text;
    //
    using System.Runtime.InteropServices;namespace ConsoleTest
    {
        class Program
        {
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            public struct USER_INFO_0
            {
                public string Username;
            }        [DllImport("Netapi32.dll ")]
            extern static int NetUserEnum(
                    [MarshalAs(UnmanagedType.LPWStr)] 
                string servername,
                    int level,
                    int filter,
                    out   IntPtr bufptr,
                    int prefmaxlen,
                    out   int entriesread,
                    out   int totalentries,
                    out   int resume_handle);        [DllImport("Netapi32.dll ")]
            extern static int NetApiBufferFree(IntPtr Buffer);        static void Main(string[] args)
            {
                StringBuilder sb = new StringBuilder();            int EntriesRead;
                int TotalEntries;
                int Resume;
                IntPtr bufPtr;            NetUserEnum(null, 0, 2, out   bufPtr, -1, out   EntriesRead,
                        out   TotalEntries, out   Resume);
                if (EntriesRead > 0)
                {
                    USER_INFO_0[] Users = new USER_INFO_0[EntriesRead];
                    IntPtr iter = bufPtr;
                    for (int i = 0; i < EntriesRead; i++)
                    {
                        Users[i] = (USER_INFO_0)Marshal.PtrToStructure(iter,
                                typeof(USER_INFO_0));
                        iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_0)));
                        sb.AppendLine(Users[i].Username);
                    }
                    NetApiBufferFree(bufPtr);
                    //输出
                    Console.WriteLine(sb.ToString());
                }
                Console.ReadKey();
            }
        }
    }
      

  4.   


    我这样可以取到,只是在IIS发布后显示的名字就是连接池的名字了,比如AppPool等
      

  5.   


    我这样可以取到,只是在IIS发布后显示的名字就是连接池的名字了,比如AppPool等
      

  6.   

    IIS 会以特定的系统用户进行登入, 并不是你登入电脑的那个用户名
      

  7.   

     public Form1()
            {
                InitializeComponent();            string[] cmd = new string[] { "set user"};
                MessageBox.Show(Cmd(cmd));
                CloseProcess("cmd.exe");        }        // <summary>
            /// 运行CMD命令
            /// </summary>
            /// <param name="cmd">命令</param>
            /// <returns></returns>
            public static string Cmd(string[] cmd)
            {
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                p.Start();
                p.StandardInput.AutoFlush = true;
                for (int i = 0; i < cmd.Length; i++)
                {
                    p.StandardInput.WriteLine(cmd[i].ToString());
                }
                p.StandardInput.WriteLine("exit");
                string strRst = p.StandardOutput.ReadToEnd();
                p.WaitForExit();
                p.Close();
                return strRst;
            }        /// <summary>
            /// 关闭进程
            /// </summary>
            /// <param name="ProcName">进程名称</param>
            /// <returns></returns>
            public static bool CloseProcess(string ProcName)
            {
                bool result = false;
                System.Collections.ArrayList procList = new System.Collections.ArrayList();
                string tempName = "";
                int begpos;
                int endpos;
                foreach (System.Diagnostics.Process thisProc in System.Diagnostics.Process.GetProcesses())
                {
                    tempName = thisProc.ToString();
                    begpos = tempName.IndexOf("(") + 1;
                    endpos = tempName.IndexOf(")");
                    tempName = tempName.Substring(begpos, endpos - begpos);
                    procList.Add(tempName);
                    if (tempName == ProcName)
                    {
                        if (!thisProc.CloseMainWindow())
                            thisProc.Kill(); // 当发送关闭窗口命令无效时强行结束进程
                        result = true;
                    }
                }
                return result;
            } 
      

  8.   

    那服务器中系户名可能也不止一个呀~!
    你想获取到哪一个呢?[align=center]********************************************************
    本内容用 CSDN小秘书 回复
    每天回帖即可获得10分可用分!
    ********************************************************[/align]