凡是支持命令行的工具,都可以通过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的典型命令:
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
msbuild
msbuild /t:Build /p:Configuration=Debug target.csproj
reactor
dotNET_Reactor.Console.exe -file client.dll -obfuscation 0 -targetfile D:\secure //最后不能有\否则报非法路径
也有很多工具集提供自己的类库、或者可以以服务的形式暴露,比如vssapi.dll或者Microsoft.Build.Engine.dll,更加方便我们的调用,并且可以得到更好的性能。
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);
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();