【发布时间】:2012-09-20 09:19:49
【问题描述】:
我正在使用 C# 在远程机器上调用 GetVolumeInformation。我可以轻松访问远程硬盘,因为默认共享设置为 c$ 或其他。但是,CD/DVD 没有默认设置。如何使用 PInvoke 调用或其他方式读取远程 CD/DVD 驱动器?
如果我不能使用 C#,我总是可以使用 PowerShell 或 WMI。
【问题讨论】:
标签: c# powershell wmi pinvoke
我正在使用 C# 在远程机器上调用 GetVolumeInformation。我可以轻松访问远程硬盘,因为默认共享设置为 c$ 或其他。但是,CD/DVD 没有默认设置。如何使用 PInvoke 调用或其他方式读取远程 CD/DVD 驱动器?
如果我不能使用 C#,我总是可以使用 PowerShell 或 WMI。
【问题讨论】:
标签: c# powershell wmi pinvoke
WMI 允许您毫无问题地获取远程机器的系统信息,只需要set the remote WMI access in the machine 并使用有效的用户名和密码。在这种情况下,您可以使用Win32_LogicalDisk 和Win32_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();
}
}
}
【讨论】:
使用 Powershell 和 WMI。
试试这个:
Get-WmiObject -computername MyremotePC Win32_CDROMDrive | Format-List *
您需要远程计算机上的管理凭据。
您可以在 powershell 中 P/invoke GetVolumeInfomation 使用 Add-Type 将其添加为类型(一些示例 here)。
如果您尝试读取未共享的远程 CD/DVD 磁盘上的数据,我不知道有什么办法。
【讨论】: