1、创建一个控制台应用程序
2、使用Nuget安装包Microsoft.Extensions.CommandLineUtils,并在Program中引用
3、编写代码
static void Main(string[] args)
{
var app = new CommandLineApplication();
app.HelpOption("-h | --help");
var optionDir = app.Option("-d|--dir <Directory>", "目录,文件夹", CommandOptionType.SingleValue);
var optionVer = app.Option("-v|--ver <Version>", "指定初始化版号本,默认1.0.0.0.0.0.0", CommandOptionType.SingleValue);
app.OnExecute(() =>
{
//获取 id 参数值
RootPath = optionDir.Value();
if (RootPath == null || RootPath.Trim().Length == 0)
Console.WriteLine("目录参数是必填的");
if (Directory.Exists(RootPath))
{
var verFile = Path.Combine(RootPath, VERSIONLISTFILE);
var version = "1.0.0.0.0.0.0";
if (optionVer != null && !string.IsNullOrEmpty(optionVer.Value()))
version = optionVer.Value();
if (File.Exists(verFile))
{
var doc = new XmlDocument();
doc.Load(verFile);
var node = doc.SelectSingleNode("VerModel//Version");
if (node != null)
version = string.Join(".", (int.Parse(node.InnerText.Replace(".", "")) 1).ToString().ToCharArray());
}
var dist = new List<FileInfo>();
GetAllFiles(RootPath, dist);
if (dist != null && dist.Count() > 0)
{
var verModel = new VerModel()
{
Version = version,
CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
VerList = new List<VerListModel>()
};
foreach (var item in dist)
{
verModel.VerList.Add(new VerListModel()
{
FileName = item.FullName.Replace(RootPath, ""),
MD5Code = ComputeFileMD5.GetMD5HashFromFile(item.FullName, out string err),
FileSize = item.Length,
FileSizeKB = Math.Round(item.Length / 1024.0m, 2)
});
}
try
{
XmlUtils.WriteFormatXML(verModel.ToXml(), FileUtils.Combine(RootPath, VERSIONLISTFILE));
Console.WriteLine($"共生成{verModel.VerList.Count}行记录");
}
catch (Exception ex)
{
log.Error(ex.Message);
Console.WriteLine(ex.Message);
}
}
}
else
{
Console.WriteLine($"目录{RootPath}不存在");
}
return 0;
});
app.Execute(args);
}
执行命令效果如下
我们也可以写成批处理,创建run.bat,键入以下命令
cd C:\jasonlee\GenVerList
GenVerList -d C:\WebPublish\Platform_Web#8083 -v 3.1.0.0
或者
D:\jasonlee\GenVerList\GenVerList.exe -d D:\Build\Web#8083 -v 3.1.1.0.0.0.0
D:\jasonlee\GenVerList\GenVerList.exe -d D:\Build\WebApi#8084 -v 3.1.1.0.0.0.0