获取当前电脑账户名称,该如何处理 发布时间: 2012-02-28 13:06:36 作者: rapoo
获取当前电脑账户名称 如桌面路径C:\Documents and Settings\Administrator\桌面 这是在管理员下面的路径 当当前账户不是管理员时 桌面路径中就不再是administrator了 成了账户名称了 如何获取账户名称呢???(不是计算机名称) 问题二: 已知文件路径,然后给这个文件创建一个快捷方式到指定目录 如给C:\Program Files\PPStream\PPStream.exe创建快捷方式到D盘下面 又该如何实现………… 谢谢高人指点[解决办法] string GetUserName() { try { string st = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo["UserName"].ToString(); } moc = null; mc = null; return st; } catch { return "unknow"; } finally { } } public void CreateShortcutLnk(string FolderPath, string PathLink, string LnkName, string LnkNote, string IconLocationPath) { try { WshShell shell = new WshShell(); IWshShortcut Shortcut = (IWshShortcut)shell.CreateShortcut(FolderPath + "\\" + LnkName + ".lnk"); Shortcut.TargetPath = PathLink; Shortcut.WindowStyle = 1; Shortcut.Description = LnkNote; Shortcut.IconLocation = IconLocationPath; Shortcut.Save(); } catch { throw new Exception("出错了"); } }参考[解决办法] 获取当前系统用户: c#.net SystemInformation.UserName vc.net SystemInformation::UserName[解决办法] char name[255]; gethostname(name, sizeof(name)); //name中存的为当前主机名[解决办法] 给出的模理模糊 不懂! 帮顶啦[解决办法]
探讨 string GetUserName()         {             try             {                 string st = "";                 ManagementClass mc = new ManagementClass("Win32_ComputerSystem");                 ManagementObjectCollection moc = mc.GetInstances();                 foreach (ManagementObject mo in moc)                 {                     st = mo["UserName"].ToString();                 }                 moc = null;                 mc = null;                 return st;             }             catch             {                 return "unknow";             }             finally             {             }         } public void CreateShortcutLnk(string FolderPath, string PathLink, string LnkName, string LnkNote, string IconLocationPath)       {             try             {               WshShell shell = new WshShell();                 IWshShortcut Shortcut = (IWshShortcut)shell.CreateShortcut(FolderPath + "\\" + LnkName + ".lnk");               Shortcut.TargetPath = PathLink;                 Shortcut.WindowStyle = 1;                 Shortcut.Description = LnkNote;                Shortcut.IconLocation = IconLocationPath;                 Shortcut.Save();             }           catch           {               throw new Exception("出错了");           }         }参考