最近由于单位发布测试密度较大,因此手动更新每个程序集([assembly: AssemblyFileVersion("1.0.0.0")])的版本号,成了很头痛的事情,因此想着能自动在原有的基础上进行累加。
因为每个人用到的版本信息可能不是同一个值,仅以我自己用到的来举例[assembly: AssemblyFileVersion("1.0.0.0")],我修改的是第二位(标红的),话不多说直接上代码:
- 首先是要选取路径(比较简单不进行赘述)
- 需要使用递归进行逐层进行文件筛选
-
调用端代码
private void GetFiles(DirectoryInfo dirInfo)
{
try
{
FileInfo[] allFile = dirInfo.GetFiles();
foreach (FileInfo fileInfo in allFile)
{
ReadFile(fileInfo.FullName);
}
DirectoryInfo[] allDir = dirInfo.GetDirectories();
foreach (DirectoryInfo dir in allDir)
{
GetFiles(dir);
}
}
catch (Exception ex)
{
MessageBox.Show("更新版本号失败!");
}
}
------------------------------------------------------------------------------------------------------------------------------------------
private void GetFiles(DirectoryInfo dirInfo)
{
try
{
FileInfo[] allFile = dirInfo.GetFiles();
foreach (FileInfo fileInfo in allFile)
{
ReadFile(fileInfo.FullName);
}
DirectoryInfo[] allDir = dirInfo.GetDirectories();
foreach (DirectoryInfo dir in allDir)
{
GetFiles(dir);
}
}
catch (Exception ex)
{
throw ex;
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
private void ReadFile(string targetPath)
{
if (!string.IsNullOrEmpty(targetPath) && !targetPath.Contains("AssemblyInfo.cs"))
{
return;
}
string pVersionName = "AssemblyFileVersion";
string content = File.ReadAllText(targetPath);
int markCount = GetMarkCountFromAllContent(content, pVersionName);
for (int i = 0; i < markCount; i++)
{
string[] arrayVersion;
string stitchingVersionString;
int versionIndex = content.IndexOf(pVersionName);
string assemblyVersion = content.Substring(versionIndex);
arrayVersion = assemblyVersion.Split('.');
if (arrayVersion.Length > 2)
{
arrayVersion[0] = arrayVersion[0].Insert(1, "##");
int assemblyVersionNumber = 0;
int.TryParse(arrayVersion[1], out assemblyVersionNumber);
arrayVersion[1] = (assemblyVersionNumber + 1).ToString();
var host = arrayVersion[0].Substring(arrayVersion[0].LastIndexOf("(") + 2);
int perVer = assemblyVersionNumber;
string hostVersion = host + "." + arrayVersion[1];
string perHostVersion = host + "." + perVer;
int index = targetPath.IndexOf("Properties");
var assimbly = targetPath.Substring(0, index - 1);
int inde = assimbly.LastIndexOf('\\');
var TypeName = assimbly.Substring(inde + 1);
if (!lstVersion.Contains(TypeName + ".dll 版本:" + hostVersion))
{
lstVersion.Add(TypeName + ".dll 版本:" + hostVersion);
txtShowInfo.Text += TypeName + ".dll 版本:" + hostVersion + " 更新前版本:" + perHostVersion + System.Environment.NewLine;
}
}
stitchingVersionString = "";
int isFirst = 0;
foreach (string version in arrayVersion)
{
if (isFirst == 0)
{
stitchingVersionString += version + ".";
isFirst++;
}
else if (isFirst == 1)
{
stitchingVersionString += version;
isFirst++;
}
else
{
stitchingVersionString += "." + version;
isFirst++;
}
}
string finishContent = content.Substring(0, versionIndex);
finishContent += stitchingVersionString;
content = finishContent;
content = content.Replace("##", "");
File.WriteAllText(targetPath, content);
}
}
/// <summary>
/// 获取打开文档中存在几处标记的数量
/// </summary>
/// <param name="pAllContent">打开文档的全部内容</param>
/// <param name="pMark">标记</param>
/// <returns></returns>
public int GetMarkCountFromAllContent(string pAllContent, string pMark)
{
if (pAllContent.Contains(pMark))
{
string strReplaced = pAllContent.Replace(pMark, "");
return (pAllContent.Length - strReplaced.Length) / pMark.Length;
}
else
{
return 0;
}
}
/// <summary>
/// 读取文件路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnReadPath_Click(object sender, EventArgs e)
{
if (fbDFolderBrowserDialog.ShowDialog() == DialogResult.OK)
{
lstVersion.Clear();
string path = fbDFolderBrowserDialog.SelectedPath;
txtPath.Text = path;
}
}
实现的功能比较单一,且通用性较差,后续继续优化。
有新的想法的小伙伴麻烦在评论区给出建议,或贴出优化后的代码..不胜感激