C#将某组件复制到某文件夹后并进行自动注册

发布时间:2010年06月01日      浏览次数:676 次
//自动注册统计图组件
CopyFile(@System.Environment.CurrentDirectory.ToString().Trim() + @"\MSCHART.OCX", @"C:\WINDOWS\system32\MSCHART.OCX"); //将组件复制到C:\WINDOWS\system32\文件夹中
RunCmd(@"regsvr32 -s C:\WINDOWS\system32\MSCHART.OCX"); //注册该组件
///////////////////////////////////////////////////////////////////////
//相关函数
///////////////////////////////////////////////////////////////////////
//注册组件
tatic string RunCmd(string strCmd)
{
string rInfo = "";
try
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo myProcessStartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.CreateNoWindow = true;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcessStartInfo.Arguments = "/c " + strCmd;
myProcess.Start();
System.IO.StreamReader myStreamReader = myProcess.StandardOutput;
rInfo = myStreamReader.ReadToEnd();
myProcess.Close();
rInfo = strCmd + "\r\n" + rInfo;
return rInfo;
}
catch { }
return rInfo;
}
//拷贝文件
static void CopyFile(string srcPath, string aimPath)
{
// 判断目标目录是否存在如果不存在则新建之
string folder = @aimPath.Substring(0, @aimPath.LastIndexOf(@"\"));
if (!System.IO.Directory.Exists(folder))
{
System.IO.Directory.CreateDirectory(folder); //如果目标文件夹不存在,则创建
}
try
{
System.IO.File.Copy(srcPath, aimPath, true); //复制文件
}
catch { }
}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!