C#WMI远程文件访问

时间:2016-02-22 11:42:02

标签: c# wmi

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        ConnectionOptions connection = new ConnectionOptions();
        connection.Username = "xxxxxx";
        connection.Password = "xxxxxx";
        connection.Authority = "ntlmdomain:xxxxxx";

        ManagementScope scope = new ManagementScope(
            "\\\\xxxxxx\\root\\CIMV2", connection);
        scope.Connect();

        ObjectQuery query = new ObjectQuery("SELECT * FROM CIM_DataFile WHERE Drive='D:' WHERE Extension='txt'");


        ManagementObjectSearcher searcher =
            new ManagementObjectSearcher(scope, query);

        foreach (ManagementObject queryObj in searcher.Get())
        {
            MessageBox.Show(queryObj["Name"].ToString());
            break;

        }

        Close();
    }
    catch (ManagementException err)
    {
        MessageBox.Show("An error occured while querying for WMI data: "
            + err.Message);
    }
    catch (System.UnauthorizedAccessException unauthorizedErr)
    {
        MessageBox.Show("Connection error " +
            "(user name or password might be incorrect): " +
            unauthorizedErr.Message);
    }
}

我正在尝试打开D Drive中的文件。这段代码对吗?这不起作用。 Button Click事件被调用两次并挂起。你能帮帮我吗?提前谢谢。

0 个答案:

没有答案