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

版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。

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

要获取的是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

【C#】获取"我的电脑"的名字,如This PC、这台计算机的更多相关文章

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

    原文:[C#]获取"我的电脑"的名字,如This PC.这台计算机 注意:这里获取的[我的电脑]的名字,不是机器的名字.如图所示: 要获取的是This PC这个字符串. ----- ...

  2. linux,shell脚本中获取脚本的名字,使用脚本的名字。

    需求描述: 写shell脚本的过程中,有时会需要获取脚本的名字,比如,有的时候,脚本 中会有usage()这种函数,可能就会用到脚本的名字. 实现方法: shell脚本中,通过使用$0就可以获取到脚本 ...

  3. js获取本机mac地址,IP地址,计算机名

    <!DOCTYPE HTML> <html> <head> <title>js获取本机mac地址,IP地址,计算机名</title> < ...

  4. 如何获取ios 设备名字 model

    由于需要获取设备名字,在网上找了一些方法,发现能够解决问题,但是需要做一个匹配,然后设备年年都会出新款,而且设备的种类又很多,所以在获取设备信息后我又做了一个操作,--->我在google上找到 ...

  5. Android编程实例-获取当前进程名字

    下面代码是根据进程id获取进程名字: /** * 根据Pid获取当前进程的名字,一般就是当前app的包名 * * @param context 上下文 * @param pid 进程的id * @re ...

  6. 获取网页是手机端还是PC端访问

    C#方式: /// <summary> /// 是否手机访问 /// </summary> /// <returns></returns> public ...

  7. 配置idea中类头注释中的 ${user} 自动获取电脑的名字,怎么去修改名字

    在idea安装路径下找到 idea\IntelliJ IDEA 2018.3.2\bin下面有一个文件叫:idea64.exe.vmoptions 编辑此文件就能修改主时钟自动获取的名称: 例如:添加 ...

  8. Python 获取一个对象的名字

    Note: 这里的对象是广义的,一个列表也算 有name的不在讨论之列, 因为可以直接输出 没有name的,可以参考如下代码,使用到的是globals() except_word是用于过滤掉那些不想要 ...

  9. java获取中问名字的首字母

    public class FirstLetterUtil { private static int BEGIN = 45217; private static int END = 63486; // ...

随机推荐

  1. Windows+anaconda+jupyter notebook+R+python3.6

    Windows+anaconda+jupyter notebook+R+python3.6 环境配置 1. 设置国内清华大学镜像 打开 anaconda prompt,输入命令 conda confi ...

  2. 洛谷P4208 [JSOI2008]最小生成树计数——题解

    题目传送 前置知识:对于同一个图的所有最小生成树,权值相等的边的数量相同. 可以简单证明一下: 我们可以从kruskal的过程考虑.这个算法把所有边按权值大小从小到大排序,然后按顺序看每条边,只要加上 ...

  3. 通过PPA存储库在UBUNTU或LINUX MINT中安装ORACLE JAVA 8 [JDK8]

    http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html sudo add-apt-repository ...

  4. 【转】博弈论——acm

    转自http://blog.csdn.net/lgdblue/article/details/15809893 序:博弈是信息学和数学试题中常会出现的一种类型,算法灵活多变是其最大特点,而其中有一类试 ...

  5. mysqli使用localhost问题

    <?php $mysqli = new mysqli('localhost', 'root', '123456', 'mysql'); if ($mysqli->connect_error ...

  6. python之新式类与经典类

    经典类与新式类 经典类:P 或 P()--深度查找,向上查父节点 新式类 :P(object)---广度查找,继承object,新式类的方法较多  

  7. Fatal error: Class 'think\db' not found

    在model层写了一个查询语句结果报错 Fatal error: Class 'think\db' not found   $list= Db::table('m_my_reserve_assess' ...

  8. SpringBoot(十二):SpringBoot整合Kafka

    https://blog.csdn.net/saytime/article/details/79950635

  9. webpack4常用片段

    webpack 4常用 初始化 npm init // Webpack 4.0以后需要单独安装 npm install webpack webpack-cli --save-dev 基础的config ...

  10. 阶段3 1.Mybatis_07.Mybatis的连接池及事务_5 mybatis中使用poolead配置连接的原理分析

    idelConnection是空闲的链接 idelConnection就是ArrayList的数组 如果没有空闲的就new一个 新的connection 一个空闲池,一个活动的池,一个链接过来.空闲池 ...