Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用WMI删除远程服务器上的文件_C#_Wmi_Wmi Query - Fatal编程技术网

C# 使用WMI删除远程服务器上的文件

C# 使用WMI删除远程服务器上的文件,c#,wmi,wmi-query,C#,Wmi,Wmi Query,我正在尝试使用WMI删除远程服务器上的现有文件 这是我的密码: string name = @"\\servername\\OCROut\\basketball.txt"; ConnectionOptions options = new ConnectionOptions(remoteServer, "username", "password", "ntlmdomain:domainName", ImpersonationLevel.Impersonate, AuthenticationLev

我正在尝试使用WMI删除远程服务器上的现有文件

这是我的密码:

string name = @"\\servername\\OCROut\\basketball.txt";

ConnectionOptions options = new ConnectionOptions(remoteServer, "username", "password", "ntlmdomain:domainName", ImpersonationLevel.Impersonate, AuthenticationLevel.Default, true, null, System.TimeSpan.MaxValue);

                            ManagementScope scope = new ManagementScope("\\\\server\\root\\cimv2", options);
                            scope.Connect();
                            var query = new ObjectQuery(string.Format("SELECT * FROM CIM_Datafile WHERE Drive = 'D' AND Name = '{0}' AND Filename = 'basketball' and Extension = 'txt'", name));
                            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
                            var tobeDeleted = searcher.Get();

                            foreach (ManagementObject item in searcher.Get())
                            {
                                item.InvokeMethod("Delete", null);
                            }
查询是工作文件,但在执行searcher.Get()方法时,我的计数为0。我尝试了一切,不同的斜杠,没有驱动器,文件名和扩展名,但似乎没有任何工作,我知道该文件存在


非常感谢您的帮助。

看来您在参数中传递了错误的值。
Name
属性必须包含文件的完整本地路径,请尝试以下操作:

string name = @"D:\\OCROut\\basketball.txt";
var query = new ObjectQuery(string.Format("SELECT * FROM CIM_Datafile WHERE Name = '{0}'", name));

用于删除远程服务器中的单个/多个文件的WMI脚本

#对于单个文件

$file = Get-WmiObject -Query "Select * from CIM_Datafile Where Name='c:\\Desktop\\a.txt'" -ComputerName 10.14.34.81 -Credential administrator
if($file)
{
     $file.delete()|out-null
}
#对于目录中的多个文件

$files =  Get-WmiObject -Query "ASSOCIATORS OF {Win32_Directory.Name='c:\Desktop\Temp'} Where ResultClass = CIM_DataFile" -ComputerName 10.14.34.81 -Credential administrator
if($files)
{
    $files|%{$_.Delete()|out-null}
}

非常感谢RRUZ…这确实有效…我想使用整个服务器名称会产生一些问题…谢谢adain buddy。我如何接受它?我试着点击向上箭头,它说我需要15个声誉?请让我知道如何,我会非常高兴地接受它…谢谢