Tôi cần đổi tên máy tính của mình qua ứng dụng .net. Tôi đã thử mã này:đổi tên máy tính theo lập trình 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;
}
}
}
nhưng không hoạt động.
và tôi đã cố gắng này:
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; }
}
nhưng nó cũng không làm việc.
"Đã không làm việc" có nghĩa là .... lỗi? – Shoban
Bạn có phải khởi động lại máy tính để thực sự phản ánh những thay đổi không? Hay bạn nhận được một số lỗi? –
@Olia Thay đổi tên máy tính thông qua các ứng dụng của bên thứ ba, nếu có thể, sẽ gây ra rất nhiều vấn đề. – Mob