在C#中使用My命名空间

以前用VB.net写过一个小程序,感觉这个My命名空间用起来确实很爽。现在学习C#,在访问应用程序设置的时候总觉得麻烦。后来在MSDN找到了在C#中使用My命名空间的方法。如下(以下内容转自MSDN):

Microsoft.VisualBasic.MyServices 命名空间(Visual Basic 中的 My)提供对许多 .NET Framework 类的简单直观的访问,使您能够编写可与计算机、应用程序、设置、资源等交互的代码。虽然 MyServices 命名空间最初是为使用 Visual Basic 而设计的,但它也可以在 C# 应用程序中使用。

有关在 Visual Basic 中使用 MyServices 命名空间的更多信息,请参见使用 My 开发。

添加引用

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

添加对 Visual Basic 库的引用
在“解决方案资源管理器”中右击“引用”节点,再选择“添加引用”。

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

您可能还需要在程序开头的 using 节中包括以下行。

C#

using Microsoft.VisualBasic.Devices;
示例

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

C#


 1 using System;
 2 using Microsoft.VisualBasic.Devices;
 3 
 4 class TestMyServices
 5 {
 6     static void Main()
 7     {
 8         // Play a sound with the Audio class:
 9         Audio myAudio = new Audio();
10         Console.WriteLine("Playing sound");
11         myAudio.Play(@"c:\WINDOWS\Media\chimes.wav");
12 
13         // Display time information with the Clock class:
14         Clock myClock = new Clock();
15         Console.Write("Current day of the week: ");
16         Console.WriteLine(myClock.LocalTime.DayOfWeek);
17         Console.Write("Current date and time: ");
18         Console.WriteLine(myClock.LocalTime);
19         
20         // Display machine information with the Computer class:
21         Computer myComputer = new Computer();
22         Console.WriteLine("Computer name: " + myComputer.Name);
23 
24         if (myComputer.Network.IsAvailable)
25         {
26             Console.WriteLine("Computer is connected to network.");
27         }
28         else
29         {
30             Console.WriteLine("Computer is not connected to network.");
31         }
32     }
33 

并不是 MyServices 命名空间中的所有的类都可以从 C# 应用程序调用:例如 FileSystemProxy 类就不兼容。在这种特定情况下,可以改用作为 FileSystem(它也包含在 VisualBasic.dll中)的一部分的静态方法。例如,下面介绍了如何使用这样的方法来复制目录:

C#

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

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/madalin/archive/2007/03/09/1525314.aspx

原文地址:https://www.cnblogs.com/jasonoiu/p/1507360.html