gpt4 book ai didi

c# - 在远程机器上创建和删除文件

转载 作者:太空宇宙 更新时间:2023-11-03 10:52:36 26 4
gpt4 key购买 nike

我想在我有管理员用户名和密码的远程机器上创建和删除一个文件。

我正在使用这个代码

ConnectionOptions options = new ConnectionOptions();
options.Username = "admin";
options.Password = "12345";
ManagementScope scope = null;
ObjectQuery query = null;
ManagementObjectSearcher searcher = null;

try
{
scope = new ManagementScope(@"\\192.168.3.125\root\CIMV2", options);
scope.Connect();
query = new ObjectQuery(@"SELECT * FROM CIM_Datafile WHERE name = 'c:\\c$\\Testing\\Test.txt'");
searcher = new ManagementObjectSearcher(scope, query); // EDIT forgot to include 'scope' previously
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return;
}

foreach(ManagementObject mo in searcher.Get())
{
uint returnCode = (uint)mo.InvokeMethod("Delete", null);
if (returnCode == 0)
Console.WriteLine("File was successfully deleted");
else
Console.WriteLine("Deletion failed due to return code " + returnCode);
}

但它给我无效的查询错误,我也想知道如何在远程机器上创建文件。

我什至无法访问路径\\192.168.3.125\C$\Testing\Test.txt

我的文件位置是c:\Testing\Test.txt

最佳答案

首先你能不能通过 native 的windows资源管理器访问这个文件(开始->运行->\192.168.3.125\C$\Testing\Test.txt)

如果是有什么问题

File.Delete(@"\\192.168.3.125\C$\Testing\Test.txt");

关于c# - 在远程机器上创建和删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20880088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com