一、命令行

凡是支持命令行的工具,都可以通过cmd.exe操作。如下:

var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
//然后就可以像操作命名行一样进行操作了
p.StandardInput.WriteLine(@"set path=%ProgramFiles%\Microsoft Visual SourceSafe;%PATH%;");
p.StandardInput.WriteLine(@"set ssdir=D:\vss");
p.StandardInput.WriteLine(@"ss CP -Yuser,pwd $/root/sln/project/");

用这个方式,就可以操作所有支持命令行的工具集。顺便附上vss、msbuild、reactor的典型命令:

  1. vss
    ss CP -Yuser,pwd $/root/sln/project/
    ss Checkout -C- -Yuser,pwd $/root/sln/project/form.cs
    ss Checkin -Crelease -Yuser,pwd $/root/sln/project/form.cs
    ss Undocheckout -I-Y -Yuser,pwd $/root/sln/project/form.cs
    ss Add -Crelease -Yuser,pwd $/root/sln/project/form.cs

  2. msbuild
    msbuild /t:Build /p:Configuration=Debug target.csproj

  3. reactor
    dotNET_Reactor.Console.exe -file client.dll -obfuscation 0 -targetfile D:\secure //最后不能有\否则报非法路径

二、用dll操作

也有很多工具集提供自己的类库、或者可以以服务的形式暴露,比如vssapi.dll或者Microsoft.Build.Engine.dll,更加方便我们的调用,并且可以得到更好的性能。

操作vss

var db = new VSSDatabase();
db.Open(@"d:\vss\srcsafe.ini", userName, pwd);
var item = db.get_VSSItem("$/root/sln/project/form.cs", false);
item.Checkout("", "", 0); // Checkin、UndoCheckout
var folder = db.get_VSSItem("$/root/sln/project", false);
var addItem = folder.Add(absolutePath, comment, 0);

操作msbuild

var engine = new Engine(@"c:\windows\Microsoft.NET\Framework\v2.0.50727");
var logger = new FileLogger();
logger.Verbosity = LoggerVerbosity.Minimal;
logger.Parameters = string.Format("logfile=log\\{0}.log", DateTime.Now.ToString("yyyyMMdd_hhmm"));
engine.RegisterLogger(logger);
var group = new BuildPropertyGroup();
group.SetProperty("Configuration", "Debug");
var result = engine.BuildProjectFile(projAbsolutePath, "Build", group);
engine.UnregisterAllLoggers();
 posted on 2015-05-18 17:08  AlexanderYao  阅读(534)  评论(0编辑  收藏  举报