bluesky_lcj

导航

一部分内容的总结

一.在命令窗口中,执行下列命令时

sc create Services_Eventlog binpath= "C:\Program Files\test\My_Calc_Frame2.0\Services_Eventlog.exe"

这个命令的作用是注册一个服务

    1.binpath=后面一定要有个空格的 否则是不能执行成功的。

  2.当路径中存在空格的时候应该用双引号把路径括起来,否则也是是不能执行成功的。

在C#语言程序中是这样的

string sysroot = System.Environment.SystemDirectory;

string targetdir = this.Context.Parameters["targetdir"].ToString();

System.Diagnostics.Process.Start(sysroot + //sc.exe, "create Services_Eventlog binpath= " + "\"" + targetdir +

"Services_Eventlog.exe" + "\"");

其中这句的作用是接受在安装程序中,相应的属性CustomActionData的值的,在使用是这个值别设置成了  【/targetdir="[TARGETDIR]\"】

string targetdir = this.Context.Parameters["targetdir"].ToString(); //取得程序安装的路径。

二.c# windows服务状态、启动和停止服务

    首先先引用System.ServiceProcess.dll ,然后在引用命名空间using System.ServiceProcess;        

    ServiceController sc = new ServiceController("Services_Eventlog");

             if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||

                     (sc.Status.Equals(ServiceControllerStatus.StopPending)))
               {
                    sc.Start();
                    sc.Refresh();
                    sc.WaitForStatus(ServiceControllerStatus.Running);
              
                }
                if (sc.Status.Equals(ServiceControllerStatus.Running))
                {
                    sc.Stop();
                    sc.Refresh();
                    sc.WaitForStatus(ServiceControllerStatus.Stopped);
                }

 3.C#读、写、删除注册表

1.首先,必须导入空间"Microsoft.Win32"2.利用Registry类,确定注册表的分支(ClassesRoot,CurrentUser,Users,LocalMachine,CurrentConfig)

using Microsoft.Win32;

 

RegistryKey key = Registry.LocalMachine;

 

3.创建/打开/删除注册表项
RegistryKey key = Registry.LocalMachine;
//创建
//注意:注册表路径一定是"\\"而不是"\"
RegistryKey software = key.CreateSubKey("software\\MyApp");

//打开(true表示可以写入)
RegistryKey software = key.OpenSubKey("software\\MyApp",true); 
RegistryKey Product 
= software.CreateSubKey("Product");

//删除
key.DeleteSubKey("software\\MyApp",true);
//记得关闭,两个都要关
key.Close();
ProductID.Close();
4.创建/打开/删除键值
RegistryKey key = Registry.LocalMachine;
RegistryKey software 
= key.OpenSubKey("software\\My",true); 
//写入
software.SetValue("ProductID""Leo Tian's blog"); 
//读取
string info = "";
info 
= software.GetValue("ProductID").ToString();
//删除
software.DeleteValue("test");
//关闭
software.Close()
5.最后贴出两个判断函数
//判断项目是否存在
private bool IsRegeditItemExist(RegistryKey RegBoot, string ItemName)
{
    
if (ItemName.IndexOf("\\"<= -1)
    {
        
string[] subkeyNames;
        subkeyNames 
= RegBoot.GetValueNames();
        
foreach (string ikeyName in subkeyNames)  //遍历整个数组
        {
            
if (ikeyName == ItemName) //判断子项的名称
            {
                
return true;
            }
        }
        
return false;               
    }
    
else
    {
        
string[] strkeyNames = ItemName.Split('\\');
        RegistryKey _newsubRegKey 
= RegBoot.OpenSubKey(strkeyNames[0]);
        
string _newRegKeyName = "";
        
int i;
        
for(i=1;i<strkeyNames.Length;i++)
        {
            _newRegKeyName 
= _newRegKeyName + strkeyNames[i];
            
if(i!=strkeyNames.Length-1)
            {
                _newRegKeyName 
= _newRegKeyName + "\\";
            }
        }
        
return IsRegeditItemExist(_newsubRegKey,_newRegKeyName);
    }
}

//判断键值是否存在
private bool IsRegeditKeyExist(RegistryKey RegBoot, string RegKeyName)
{

    
string[] subkeyNames;
    subkeyNames 
= RegBoot.GetValueNames();
    
foreach (string keyName in subkeyNames)
    {

        
if (keyName == RegKeyName)  //判断键值的名称
        {
            
return true;
        }
    }
    
return false;
}

 

四.C# 读取XML中CDATA中的数据

例如有Config.xml文件 内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<Config>
  <log>
    <path><![CDATA[C:\test\emv\Log]]></path>
    <level>
      <console>0x7</console>
      <file>0xf</file>
    </level>
  </log>
</Config>

对应C#的程序如下:

 XmlDataDocument xDoc = new XmlDataDocument();
 xDoc.Load("Config.xml");
 XmlNodeList xNodeList = xDoc.SelectNodes("//Config/log/path");
 String logPath = xNodeList.Item(0).InnerText;

Console.WriteLine(logPath);

 

 

 

 

 

 

 

posted on 2009-04-09 17:40  bluesky_lcj  阅读(164)  评论(0编辑  收藏  举报