c# 如何使用 My 命名空间

作者:olprod 时间:2022-12-25 20:23:39 

Microsoft.VisualBasic.MyServices 命名空间(在 Visual Basic 中为 My)使访问多个 .NET 类变得轻松直观,让你能够编写与计算机、应用程序、设置、资源等交互的代码。 虽然最初设计用于 Visual Basic,但 MyServices 命名空间仍可用于 C# 应用程序。

添加引用

可以在解决方案中使用 MyServices 类之前,必须添加对 Visual Basic 库的引用。

添加对 Visual Basic 库的引用

  1. 在解决方案资源管理器中,右键单击“引用”节点并选择“添加引用” 。

  2. 出现“引用”对话框时,向下滚动列表,然后选择“Microsoft.VisualBasic.dll”。

同时建议将以下行包括在程序开头的 using 部分。


using Microsoft.VisualBasic.Devices;

示例

此示例调用 MyServices 命名空间中包含的各种静态方法。 若要编译此代码,必须向项目添加对 Microsoft.VisualBasic.DLL 的引用。


using System;
using Microsoft.VisualBasic.Devices;

class TestMyServices
{
static void Main()
{
 // Play a sound with the Audio class:
 Audio myAudio = new Audio();
 Console.WriteLine("Playing sound...");
 myAudio.Play(@"c:\WINDOWS\Media\chimes.wav");

// Display time information with the Clock class:
 Clock myClock = new Clock();
 Console.Write("Current day of the week: ");
 Console.WriteLine(myClock.LocalTime.DayOfWeek);
 Console.Write("Current date and time: ");
 Console.WriteLine(myClock.LocalTime);

// Display machine information with the Computer class:
 Computer myComputer = new Computer();
 Console.WriteLine("Computer name: " + myComputer.Name);

if (myComputer.Network.IsAvailable)
 {
  Console.WriteLine("Computer is connected to network.");
 }
 else
 {
  Console.WriteLine("Computer is not connected to network.");
 }
}
}

并不是 MyServices 命名空间中的所有类均可从 C# 应用程序中调用:例如,FileSystemProxy 类不兼容。 在此特定情况下,可以改为使用属于 FileSystem 的静态方法,这些方法也包含在 VisualBasic.dll 中。 例如,下面介绍了如何使用此类方法来复制目录:


// Duplicate a directory
Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(
@"C:\original_directory",
@"C:\copy_of_original_directory");

来源:https://github.com/dotnet/docs.zh-cn/blob/live/docs/csharp/programming-guide/namespaces/how-to-use-the-my-namespace.md

标签:c#,my,命名空间
0
投稿

猜你喜欢

  • java15新功能的详细讲解

    2023-08-23 04:40:21
  • C#表达式中的动态查询详解【译】

    2021-07-29 05:19:42
  • Android 顶部标题栏随滑动时的渐变隐藏和渐变显示效果

    2023-11-26 07:48:39
  • android图像绘制(四)自定义一个SurfaceView控件

    2022-09-27 11:28:07
  • Android使用PowerImageView实现播放强大的ImageView动画效果

    2022-06-16 19:55:54
  • android多媒体音乐(MediaPlayer)播放器制作代码

    2022-01-06 01:13:20
  • C# form-data上传图片流到远程服务器的详细代码

    2022-06-12 01:39:58
  • C#中变量、常量、枚举、预处理器指令知多少

    2021-05-26 18:29:11
  • Android性能优化之Bitmap图片优化详解

    2023-08-06 02:54:23
  • SpringBoot-Admin实现微服务监控+健康检查+钉钉告警

    2021-12-23 02:40:22
  • Java流程控制语句之If选择结构

    2023-11-11 04:02:29
  • C# 泛型List排序的实现

    2021-09-22 07:03:16
  • PC蓝牙通信C#代码实现

    2023-07-06 19:59:32
  • MyBatis动态SQL标签的用法详解

    2021-07-24 10:38:56
  • android使用 ScrollerView 实现 可上下滚动的分类栏实例

    2022-11-03 16:38:57
  • C#数字图像处理之图像缩放的方法

    2023-02-12 11:09:29
  • SpringBoot整合腾讯云COS对象存储实现文件上传的示例代码

    2021-06-01 03:25:14
  • SpringBoot使用GraphQL开发Web API实现方案示例讲解

    2023-05-17 16:50:22
  • JVM Tomcat性能实战(推荐)

    2022-02-11 22:34:02
  • Java加载资源文件时的路径问题的解决办法

    2023-05-09 23:22:40
  • asp之家 软件编程 m.aspxhome.com