2011-10-04 37 views
8

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.

+4

"Đã không làm việc" có nghĩa là .... lỗi? – Shoban

+0

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? –

+0

@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

Trả lời

6

Tôi đã thử tất cả các cách tôi đã tìm thấy để thay đổi máy tính tên và không ai hoạt động ..... nó không thay đổi tên máy tính ... cách duy nhất nó làm việc là khi tôi đã tổ chức một số giá trị khóa đăng ký, đây là mã, là nó ok để làm như vậy?

public static bool SetMachineName(string newName) 
{ 
    RegistryKey key = Registry.LocalMachine; 

    string activeComputerName = "SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName"; 
    RegistryKey activeCmpName = key.CreateSubKey(activeComputerName); 
    activeCmpName.SetValue("ComputerName", newName); 
    activeCmpName.Close(); 
    string computerName = "SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName"; 
    RegistryKey cmpName = key.CreateSubKey(computerName); 
    cmpName.SetValue("ComputerName", newName); 
    cmpName.Close(); 
    string _hostName = "SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\"; 
    RegistryKey hostName = key.CreateSubKey(_hostName); 
    hostName.SetValue("Hostname",newName); 
    hostName.SetValue("NV Hostname",newName); 
    hostName.Close(); 
    return true; 
} 

và sau khi khởi động lại tên thay đổi ....

3

Từ các tài liệu MSDN của SetComputerName ..

Thiết lập một tên NetBIOS mới cho máy tính cục bộ. Tên được lưu trữ trong sổ đăng ký và thay đổi tên sẽ có hiệu lực vào lần sau khi người dùng khởi động lại máy tính.

Bạn đã thử khởi động lại máy tính chưa?

+0

có ... tôi đã khởi động lại –

1

Programmatically renaming a computer using C#

Nó là một bài viết dài và tôi không chắc chắn chính xác những gì sẽ liên quan trực tiếp vì vậy tôi sẽ không dán một đoạn

+3

Mặc dù điều này về mặt lý thuyết có thể trả lời câu hỏi, [nó sẽ là thích hợp hơn] (http: //meta.stackexchange.com/q/8259) để bao gồm các phần thiết yếu của câu trả lời ở đây và cung cấp liên kết để tham khảo. –

+0

chức năng trong bài viết này, thay đổi tên của máy tính, ném ngoại lệ này - Không tìm thấy đường dẫn mạng. (Ngoại lệ từ HRESULT: 0x80070035) –

2

đối tượng Một WMI đặt tên máy tính. Sau đó, đăng ký được sử dụng để kiểm tra xem tên đã được đặt chưa. Bởi vì System.Environment.MachineName không được cập nhật ngay lập tức. Và lệnh 'tên máy chủ' trong CMD.exe vẫn xuất ra tên cũ. Vì vậy, khởi động lại vẫn là bắt buộc. Nhưng với việc kiểm tra đăng ký có thể xem nếu tên đã được thiết lập.

Hy vọng điều này sẽ hữu ích.

Boolean SetComputerName(String Name) 
{ 
String RegLocComputerName = @"SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName"; 
try 
{ 
    string compPath= "Win32_ComputerSystem.Name='" + System.Environment.MachineName + "'"; 
    using (ManagementObject mo = new ManagementObject(new ManagementPath(compPath))) 
    { 
     ManagementBaseObject inputArgs = mo.GetMethodParameters("Rename"); 
     inputArgs["Name"] = Name; 
     ManagementBaseObject output = mo.InvokeMethod("Rename", inputArgs, null); 
     uint retValue = (uint)Convert.ChangeType(output.Properties["ReturnValue"].Value, typeof(uint)); 
     if (retValue != 0) 
     { 
      throw new Exception("Computer could not be changed due to unknown reason."); 
     } 
    } 

    RegistryKey ComputerName = Registry.LocalMachine.OpenSubKey(RegLocComputerName); 
    if (ComputerName == null) 
    { 
     throw new Exception("Registry location '" + RegLocComputerName + "' is not readable."); 
    } 
    if (((String)ComputerName.GetValue("ComputerName")) != Name) 
    { 
     throw new Exception("The computer name was set by WMI but was not updated in the registry location: '" + RegLocComputerName + "'"); 
    } 
    ComputerName.Close(); 
    ComputerName.Dispose(); 
} 
catch (Exception ex) 
{ 
    return false; 
} 
return true; 
} 
0

Rất khó cập nhật tên PC bằng bất kỳ phương pháp bên ngoài nào do bảo vệ hệ thống. Cách tốt nhất để làm như vậy nó để sử dụng các cửa sổ tiện ích riêng của WMIC.exe để đổi tên máy tính. Chỉ cần khởi chạy wmic.exe từ C# và chuyển lệnh đổi tên thành đối số.

>

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); 
    } 
} 
+0

Câu hỏi của bạn là gì? Điều gì dường như là vấn đề cụ thể mà bạn đang gặp phải? – TVOHM

Các vấn đề liên quan