使用PInvoke捕获远程机器的CD Rom信息

时间:2012-09-28 13:21:26

标签: c# powershell wmi pinvoke

我正在使用C#在远程计算机上调用GetVolumeInformation。我可以轻松点击远程硬盘驱动器,因为有一个c $或其他的默认共享设置。但是,CD / DVD没有默认设置。如何使用PInvoke调用或其他方式读取远程CD / DVD驱动器?

如果我不能使用C#,我总是可以使用PowerShell或WMI。

2 个答案:

答案 0 :(得分:2)

WMI允许您毫无问题地获取远程计算机的系统信息,只需要set the remote WMI access in the machine并使用有效的用户和密码。在这种情况下,您可以使用Win32_LogicalDiskWin32_CDROMDrive类来检索所需的信息。

试试这个C#样本。

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                string ComputerName = "localhost";//set the remote machine name here
                ManagementScope Scope;                

                if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase)) 
                {
                    ConnectionOptions Conn = new ConnectionOptions();
                    Conn.Username  = "";//user
                    Conn.Password  = "";//password
                    Conn.Authority = "ntlmdomain:DOMAIN";
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn);
                }
                else
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);

                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_CDROMDrive");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    Console.WriteLine("{0,-35} {1,-40}","DeviceID",WmiObject["DeviceID"]);// String
                    Console.WriteLine("{0,-35} {1,-40}","Drive",WmiObject["Drive"]);// String

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }

}

答案 1 :(得分:1)

使用Powershell和WMI。

试试这个:

 Get-WmiObject -computername MyremotePC Win32_CDROMDrive | Format-List *

您需要远程计算机上的管理凭据。

您可以在powershell中调用GetVolumeInfomation使用Add-Type将其添加为类型(某些示例here)。

如果您正在尝试读取未共享的远程CD / DVD磁盘上的数据,我不知道该怎么做。