使用 .NET 重命名计算机名称
Rename computer name with .NET
我正在尝试从 C# 应用程序重命名计算机名称。
public class ComputerSystem : IComputerSystem
{
private readonly ManagementObject computerSystemObject;
public ComputerSystem()
{
var computerPath = string.Format("Win32_ComputerSystem.Name='{0}'", Environment.MachineName);
computerSystemObject = new ManagementObject(new ManagementPath(computerPath));
}
public bool Rename(string newComputerName)
{
var result = false;
var renameParameters = computerSystemObject.GetMethodParameters("Rename");
renameParameters["Name"] = newComputerName;
var output = computerSystemObject.InvokeMethod("Rename", renameParameters, null);
if (output != null)
{
var returnValue = (uint)Convert.ChangeType(output.Properties["ReturnValue"].Value, typeof(uint));
result = returnValue == 0;
}
return result;
}
}
WMI 调用 returns 错误代码 1355。
MSDN 没有过多提及错误代码,它是什么意思,我该如何解决?
Error code 1355 表示 ERROR_NO_SUCH_DOMAIN
: "The specified domain either does not exist or could not be contacted.".
documentation for the Rename method 声明名称 必须 包含域名。对于未加入域的计算机,请尝试 .\NewName
而不是 NewName
.
由于系统保护,很难使用任何外部方法更新PC 名称。这样做的最佳方法是使用 Windows 自带的 WMIC.exe 实用程序来重命名 PC。只需从 C# 启动 wmic.exe 并将重命名命令作为参数传递。
退出代码 0
>
public void SetMachineName(string newName)
{
// Create a new process
ProcessStartInfo process = new ProcessStartInfo();
// set name of process to "WMIC.exe"
process.FileName = "WMIC.exe";
// pass rename PC command as argument
process.Arguments = "computersystem where caption='" + System.Environment.MachineName + "' rename " + newName;
// Run the external process & wait for it to finish
using (Process proc = Process.Start(process))
{
proc.WaitForExit();
// print the status of command
Console.WriteLine("Exit code = " + proc.ExitCode);
}
}
我正在尝试从 C# 应用程序重命名计算机名称。
public class ComputerSystem : IComputerSystem
{
private readonly ManagementObject computerSystemObject;
public ComputerSystem()
{
var computerPath = string.Format("Win32_ComputerSystem.Name='{0}'", Environment.MachineName);
computerSystemObject = new ManagementObject(new ManagementPath(computerPath));
}
public bool Rename(string newComputerName)
{
var result = false;
var renameParameters = computerSystemObject.GetMethodParameters("Rename");
renameParameters["Name"] = newComputerName;
var output = computerSystemObject.InvokeMethod("Rename", renameParameters, null);
if (output != null)
{
var returnValue = (uint)Convert.ChangeType(output.Properties["ReturnValue"].Value, typeof(uint));
result = returnValue == 0;
}
return result;
}
}
WMI 调用 returns 错误代码 1355。
MSDN 没有过多提及错误代码,它是什么意思,我该如何解决?
Error code 1355 表示 ERROR_NO_SUCH_DOMAIN
: "The specified domain either does not exist or could not be contacted.".
documentation for the Rename method 声明名称 必须 包含域名。对于未加入域的计算机,请尝试 .\NewName
而不是 NewName
.
由于系统保护,很难使用任何外部方法更新PC 名称。这样做的最佳方法是使用 Windows 自带的 WMIC.exe 实用程序来重命名 PC。只需从 C# 启动 wmic.exe 并将重命名命令作为参数传递。
退出代码 0
>
public void SetMachineName(string newName)
{
// Create a new process
ProcessStartInfo process = new ProcessStartInfo();
// set name of process to "WMIC.exe"
process.FileName = "WMIC.exe";
// pass rename PC command as argument
process.Arguments = "computersystem where caption='" + System.Environment.MachineName + "' rename " + newName;
// Run the external process & wait for it to finish
using (Process proc = Process.Start(process))
{
proc.WaitForExit();
// print the status of command
Console.WriteLine("Exit code = " + proc.ExitCode);
}
}