SoFunction
Updated on 2025-03-07

How to use My namespace

Namespaces (My in Visual Basic) make access to multiple .NET classes easy and intuitive, allowing you to write code that interacts with computers, applications, settings, resources, and more. Although originally designed for Visual Basic,MyServicesThe namespace is still available for C# applications.

Add a reference

Can be used in the solutionMyServices Before the class, a reference to the Visual Basic library must be added.

Add a reference to the Visual Basic library

  1. In Solution Explorer, right-click the Reference node and select Add Reference.
  2. When the Reference dialog box appears, scroll down the list and select ".

It is also recommended to include the following line in the using section at the beginning of the program.

using ;

Example

This example calls various static methods included in the MyServices namespace. To compile this code, you must add a reference to the project.

using System;
using ;

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

  // Display time information with the Clock class:
  Clock myClock = new Clock();
  ("Current day of the week: ");
  ();
  ("Current date and time: ");
  ();

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

  if ()
  {
   ("Computer is connected to network.");
  }
  else
  {
   ("Computer is not connected to network.");
  }
 }
}

NotMyServicesAll classes in the namespace can be called from a C# application: For example, the FileSystemProxy class is incompatible. In this particular case, you can instead use static methods belonging to FileSystem, which are also included in it. For example, the following describes how to use this method to copy a directory:

// Duplicate a directory
(
 @"C:\original_directory",
 @"C:\copy_of_original_directory");

The above is the detailed content of how to use the My namespace in c#. For more information about the c# namespace, please follow my other related articles!