服务DisplayName以编程方式更改

服务DisplayName以编程方式更改

本文介绍了服务DisplayName以编程方式更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

当我以编程方式更改Service DisplayName时,该服务将成为注销者,但是当更改Manully时,为什么没有问题呢?

我想通过vb.net Prorame来更改DisplayName
My.Computer.Registry.SetValue("hkey_local_machine \ system \ currentcontrolset \ services \ GUI","DisplayName",txtServiceName.Text)

请建议我,我该如何解决这个问题?

问候
Ravi Kumar先生

dear friends,

When i change Service DisplayName Programmatically then the service Became Unregistor, but when change Manully then there is no problem why?

I want to chane DisplayName by vb.net Prorame
My.Computer.Registry.SetValue("hkey_local_machine\system\currentcontrolset\services\GUI", "DisplayName", txtServiceName.Text)

Please Suggest me, How can i Solve this Problem ?

Regards
Mr. Ravi Kumar

推荐答案

private void ChangeServiceName(string oldName, string newName)
        {
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.Arguments = string.Format("/c sc config {0} displayname= {1}", oldName, newName);
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.FileName = "cmd.exe";
            string output = "";
            string error = "";


            try
            {
                p.Start();
                p.WaitForExit();
                output = p.StandardOutput.ReadToEnd();
                error = p.StandardError.ReadToEnd();
            }

            catch (Exception ex)
            {

                error = ex.ToString();
            }
        }




VB代码示例,请进行适当修改:




VB Code sample, please modify appropriately:

Public Sub RenameService(ByVal OldName As String, ByVal NewName As String)
        Dim objProcess As System.Diagnostics.Process

        Try
            objProcess = New System.Diagnostics.Process()
            objProcess.StartInfo.FileName = "cmd.exe"
            objProcess.StartInfo.CreateNoWindow = True
            objProcess.StartInfo.Arguments = "/c sc config " & OldName & " displayname= " & NewName
            objProcess.StartInfo.RedirectStandardOutput = True
            objProcess.StartInfo.RedirectStandardError = True
            Dim stringOutput As String = ""
            Dim stringError As String = ""
            objProcess.Start()

            'Wait until the process passes back an exit code
            objProcess.WaitForExit()

            'Free resources associated with this process
            objProcess.Close()
        Catch
            MessageBox.Show("Could not start process " & ProcessPath, "Error")
        End Try
    End Sub


这篇关于服务DisplayName以编程方式更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:27