Programmatically close optical drive tray
Note that the Visual Studio C# code editor has an integrated code beautifier. Depending on your setup, it can be executed with different shortcut keys. Call it from the menu for the first time, so that you can see the active shortcut keys (it is Ctrl-E-D for me). Menu: Edit &gt; Advanced &gt; Format Document.
By using a technique called LINQ (Language INtegrated  Query), you can get the management object easier. It uses extension methods from the namespace System.Linq. Therefore, you must include a using System.Linq; at the top of your code. Then you can query with
ManagementObject managementObject = moc.FirstOrDefault();
if (managementObject == null) {
     // Handle error (output text or throw exception)
     Console.WriteLine(&quot;Could not find a management object!&quot;);
} else {
     ...
}

This FirstOrDefault extension method calls GetEnumerator and MoveNext internally and returns null if no object is available.
Do not give it the name obj. Almost everything is an object, so this name is not very informative.
Exceptions are a complex matter. It raises a lot of questions like &quot;should I throw an exception or output a message to the user?&quot;, &quot;should I create my own exception types?&quot;, &quot;should I log the exception?&quot; etc., etc. There is no single best answer to these questions. Therefore, I content myself with giving you a link: C# Exception Handling Best Practices.
If you intend to reuse this functionality, you could encapsulate it into your own class:
public enum OperationResult
{
    Failure,
    NoDriveFound,
    DriveIsOpenOrEmpty,
    MediaIsLoaded
}

public class OpticalDriveCloser
{
    public OperationResult CloseFirst()
    {
        ...
    }

    public OperationResult[] CloseAll()
    {
        ...
    }
}

It returns an operation status as enumeration type and lets the calling application decide how to proceed. I.e. it does neither call Console.WriteLine nor throw exceptions and focuses on pure, non-UI logic. A console application will have a different UI-logic than a windows forms application or a web application.