【C#】获取"我的电脑"的名字,如This PC、这台计算机

简介: 原文:【C#】获取"我的电脑"的名字,如This PC、这台计算机 注意:这里获取的【我的电脑】的名字,不是机器的名字。
原文: 【C#】获取"我的电脑"的名字,如This PC、这台计算机

注意:这里获取的【我的电脑】的名字,不是机器的名字。如图所示:


要获取的是This PC这个字符串。

-------------------------------------------------------------------

机器的名字可以通过系统属性查看,叫做Computer Name,如下图:


我的就是cn-c-w-725,如果想获取这个字符串直接调用Environment.MachineName就行了。

1.定义ShellAPI

 public static class ShellAPI
    {

        public const int MAX_PATH = 260;
        public const uint CMD_FIRST = 1;
        public const uint CMD_LAST = 30000;

        public const int S_OK = 0, S_FALSE = 1;
        public const int DRAGDROP_S_DROP = 0x00040100;
        public const int DRAGDROP_S_CANCEL = 0x00040101;
        public const int DRAGDROP_S_USEDEFAULTCURSORS = 0x00040102;

        public static int cbFileInfo = Marshal.SizeOf(typeof(SHFILEINFO));



        // Retrieves information about an object in the file system,
        // such as a file, a folder, a directory, or a drive root.
        [DllImport("shell32",
            EntryPoint = "SHGetFileInfo",
            ExactSpelling = false,
            CharSet = CharSet.Auto,
            SetLastError = true)]
        public static extern IntPtr SHGetFileInfo(
            IntPtr ppidl,
            FILE_ATTRIBUTE dwFileAttributes,
            ref SHFILEINFO sfi,
            int cbFileInfo,
            SHGFI uFlags);

        [DllImport("Shell32",
            EntryPoint = "SHGetSpecialFolderLocation",
            ExactSpelling = true,
            CharSet = CharSet.Ansi,
            SetLastError = true)]
        public static extern Int32 SHGetSpecialFolderLocation(
            IntPtr hwndOwner,
            CSIDL nFolder,
            out IntPtr ppidl);

        // This function takes the fully-qualified pointer to an item
        // identifier list (PIDL) of a namespace object, and returns a specified
        // interface pointer on the parent object.


        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct SHFILEINFO
        {
            public IntPtr hIcon;
            public int iIcon;
            public SFGAO dwAttributes;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
            public string szDisplayName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
            public string szTypeName;
        }
        // Flags that specify the file information to retrieve with SHGetFileInfo
        [Flags]
        public enum FILE_ATTRIBUTE
        {
            READONLY = 0x00000001,
            HIDDEN = 0x00000002,
            SYSTEM = 0x00000004,
            DIRECTORY = 0x00000010,
            ARCHIVE = 0x00000020,
            DEVICE = 0x00000040,
            NORMAL = 0x00000080,
            TEMPORARY = 0x00000100,
            SPARSE_FILE = 0x00000200,
            REPARSE_POINT = 0x00000400,
            COMPRESSED = 0x00000800,
            OFFLINE = 0x00001000,
            NOT_CONTENT_INDEXED = 0x00002000,
            ENCRYPTED = 0x00004000
        }
        // The attributes that the caller is requesting, when calling IShellFolder::GetAttributesOf
        [Flags]
        public enum SFGAO : uint
        {
            BROWSABLE = 0x8000000,
            CANCOPY = 1,
            CANDELETE = 0x20,
            CANLINK = 4,
            CANMONIKER = 0x400000,
            CANMOVE = 2,
            CANRENAME = 0x10,
            CAPABILITYMASK = 0x177,
            COMPRESSED = 0x4000000,
            CONTENTSMASK = 0x80000000,
            DISPLAYATTRMASK = 0xfc000,
            DROPTARGET = 0x100,
            ENCRYPTED = 0x2000,
            FILESYSANCESTOR = 0x10000000,
            FILESYSTEM = 0x40000000,
            FOLDER = 0x20000000,
            GHOSTED = 0x8000,
            HASPROPSHEET = 0x40,
            HASSTORAGE = 0x400000,
            HASSUBFOLDER = 0x80000000,
            HIDDEN = 0x80000,
            ISSLOW = 0x4000,
            LINK = 0x10000,
            NEWCONTENT = 0x200000,
            NONENUMERATED = 0x100000,
            READONLY = 0x40000,
            REMOVABLE = 0x2000000,
            SHARE = 0x20000,
            STORAGE = 8,
            STORAGEANCESTOR = 0x800000,
            STORAGECAPMASK = 0x70c50008,
            STREAM = 0x400000,
            VALIDATE = 0x1000000
        }
        // Used to retrieve directory paths to system special folders
        public enum CSIDL
        {
            ADMINTOOLS = 0x30,
            ALTSTARTUP = 0x1d,
            APPDATA = 0x1a,
            BITBUCKET = 10,
            CDBURN_AREA = 0x3b,
            COMMON_ADMINTOOLS = 0x2f,
            COMMON_ALTSTARTUP = 30,
            COMMON_APPDATA = 0x23,
            COMMON_DESKTOPDIRECTORY = 0x19,
            COMMON_DOCUMENTS = 0x2e,
            COMMON_FAVORITES = 0x1f,
            COMMON_MUSIC = 0x35,
            COMMON_PICTURES = 0x36,
            COMMON_PROGRAMS = 0x17,
            COMMON_STARTMENU = 0x16,
            COMMON_STARTUP = 0x18,
            COMMON_TEMPLATES = 0x2d,
            COMMON_VIDEO = 0x37,
            CONTROLS = 3,
            COOKIES = 0x21,
            DESKTOP = 0,
            DESKTOPDIRECTORY = 0x10,
            DRIVES = 0x11,
            FAVORITES = 6,
            FLAG_CREATE = 0x8000,
            FONTS = 20,
            HISTORY = 0x22,
            INTERNET = 1,
            INTERNET_CACHE = 0x20,
            LOCAL_APPDATA = 0x1c,
            MYDOCUMENTS = 12,
            MYMUSIC = 13,
            MYPICTURES = 0x27,
            MYVIDEO = 14,
            NETHOOD = 0x13,
            NETWORK = 0x12,
            PERSONAL = 5,
            PRINTERS = 4,
            PRINTHOOD = 0x1b,
            PROFILE = 40,
            PROFILES = 0x3e,
            PROGRAM_FILES = 0x26,
            PROGRAM_FILES_COMMON = 0x2b,
            PROGRAMS = 2,
            RECENT = 8,
            SENDTO = 9,
            STARTMENU = 11,
            STARTUP = 7,
            SYSTEM = 0x25,
            TEMPLATES = 0x15,
            WINDOWS = 0x24
        }

        // Defines the values used with the IShellFolder::GetDisplayNameOf and IShellFolder::SetNameOf 
        // methods to specify the type of file or folder names used by those methods

        // Flags that specify the file information to retrieve with SHGetFileInfo
        [Flags]
        public enum SHGFI : uint
        {
            ADDOVERLAYS = 0x20,
            ATTR_SPECIFIED = 0x20000,
            ATTRIBUTES = 0x800,
            DISPLAYNAME = 0x200,
            EXETYPE = 0x2000,
            ICON = 0x100,
            ICONLOCATION = 0x1000,
            LARGEICON = 0,
            LINKOVERLAY = 0x8000,
            OPENICON = 2,
            OVERLAYINDEX = 0x40,
            PIDL = 8,
            SELECTED = 0x10000,
            SHELLICONSIZE = 4,
            SMALLICON = 1,
            SYSICONINDEX = 0x4000,
            TYPENAME = 0x400,
            USEFILEATTRIBUTES = 0x10
        }

    }


2.调用输出显示

 static void Main(string[] args)
        {
            IntPtr tempPidl;
            ShellAPI.SHFILEINFO info;
            
            //My Computer
            info = new ShellAPI.SHFILEINFO();
            tempPidl = IntPtr.Zero;
            ShellAPI.SHGetSpecialFolderLocation(IntPtr.Zero, ShellAPI.CSIDL.DRIVES, out tempPidl);

            ShellAPI.SHGetFileInfo(tempPidl, 0, ref info, ShellAPI.cbFileInfo,
                ShellAPI.SHGFI.PIDL | ShellAPI.SHGFI.DISPLAYNAME | ShellAPI.SHGFI.TYPENAME);

            string sysfolderName = info.szTypeName;
            string mycompName = info.szDisplayName;
            Console.WriteLine(sysfolderName + "------" + mycompName);
            Console.WriteLine(Environment.MachineName);
            Console.Read();
        }

mycompName就是我们想要的值.

3.再介绍一个用winform编写的功能很强大的文件管理器。与自带的差不多,调用了系统的Shell api,可以获取到特殊路径的名称,对于没有权限操作的文件会进行动态请求。如图:


CodeProject地址

如果打不开,请去csdn下载


进阶-参考另外一篇文章《【C#】WindowsAPICodePack-Shell使用教程》,地址:http://blog.csdn.net/catshitone/article/details/72723927


目录
相关文章
|
7月前
|
存储 传感器 监控
Baumer工业相机堡盟工业相机如何通过BGAPISDK将相机图像高速保存到电脑内存(C#)
Baumer工业相机堡盟工业相机如何通过BGAPISDK将相机图像高速保存到电脑内存(C#)
106 0
|
7月前
|
监控 安全 C#
开发公司电脑监控软件的报警系统:一个C#示例
在当今数字化时代,企业对其计算机网络和系统的安全性和稳定性越来越重视。为了确保员工遵守公司政策、保护机密信息以及监控系统的正常运行,开发一种可靠的公司电脑监控软件变得至关重要。本文将介绍如何使用C#编写一个简单而有效的报警系统,以便监控关键数据并在必要时发出警报。
193 0
【C#】【FFmpeg】获取电脑可用音视频设备并输出到下拉列表框
【C#】【FFmpeg】获取电脑可用音视频设备并输出到下拉列表框
309 0
【C#】【FFmpeg】获取电脑可用音视频设备并输出到下拉列表框
|
C# Windows
用 C# 代码如何实现让你的电脑关机,重启,注销,锁定,休眠,睡眠
原文:用 C# 代码如何实现让你的电脑关机,重启,注销,锁定,休眠,睡眠 简介 本文讲述了用 C# 代码如何实现让你的电脑关机,重启,注销,锁定,休眠,睡眠。 如何实现 首先,使用 using 语句添加我们需要的命名空间: using System.
3609 0
|
存储 C#
C#修改电脑桌面图
C#修改电脑桌面图
142 0
C# 获取电脑MAC地址,IP地址,物理内存,CPU序列号,硬盘ID..........................
上班很忙,自己做个记录 代码如下: 需要引入:System.Management 代码如下: using System; using System.Collections.Generic; using System.
1544 0
|
JavaScript Go C#
C#判断用户是手机访问还是PC访问
今天在做一个wap网站时,需要限制PC用户访问。网上找了很多资料,效果都不怎么理想。其实原理就是根据HTTP_USER_AGENT判断检查用户在用什么浏览器,再根据业务做相应的逻辑处理。     代码如下: 1 public class IsPhoneAttribute : A...
1305 0
C#判断联网状态检查电脑联网状态
C#判断检查联网状态-判断Unity Window PC电脑联网状态 本文提供全流程,中文翻译 Unity判断联网状态,适用于所有平台 助力快速利用 C# 判断用户电脑网状态 为新手节省宝贵的时间,避免采坑! Unity 引擎提供的 Application.
2733 0
|
机器学习/深度学习 C# API
C# WPF 低仿网易云音乐(PC)歌词控件
原文:C# WPF 低仿网易云音乐(PC)歌词控件 提醒:本篇博客记录了修改的过程,废话比较多,需要项目源码和看演示效果的直接拉到文章最底部~   网易云音乐获取歌词的api地址 http://music.
2103 0
|
C# Windows
C# WPF 低仿网易云音乐(PC)Banner动画控件
原文:C# WPF 低仿网易云音乐(PC)Banner动画控件 由于技术有限没能做到一模一样的动画,只是粗略地做了一下。动画有点生硬,还有就是没做出网易云音乐的立体感。代码非常简单粗暴,而且我也写有很多注释,这里就不多啰嗦了,直接贴代码。
1577 0