我需要通過.net應用程序重命名我的計算機。 我試過這個代碼:以編程方式重命名計算機c#.net
public static bool SetMachineName(string newName)
{
MessageBox.Show(String.Format("Setting Machine Name to '{0}'...", newName));
// Invoke WMI to populate the machine name
using (ManagementObject wmiObject = new ManagementObject(new ManagementPath(String.Format("Win32_ComputerSystem.Name='{0}'",System.Environment.MachineName))))
{
ManagementBaseObject inputArgs = wmiObject.GetMethodParameters("Rename");
inputArgs["Name"] = newName;
// Set the name
ManagementBaseObject outParams = wmiObject.InvokeMethod("Rename",inputArgs,null);
uint ret = (uint)(outParams.Properties["ReturnValue"].Value);
if (ret == 0)
{
//worked
return true;
}
else
{
//didn't work
return false;
}
}
}
但它沒有工作。
,我已經試過這一個:
using System.Runtime.InteropServices;
[DllImport("kernel32.dll")]
static extern bool SetComputerName(string lpComputerName);
public static bool SetMachineName(string newName)
{
bool done = SetComputerName(newName);
if (done)
{
{ MessageBox.Show("Done"); return true; }
}
else
{ MessageBox.Show("Failed"); return false; }
}
,但它也沒有工作。
「沒有工作」 的意思....錯誤? – Shoban
您是否必須重新啓動計算機才能真正反映這些更改?或者你有一些錯誤? –
@Olia如果可能的話,通過第三方應用更改計算機名稱會導致很多問題。 – Mob