Can I automatically increment the file build version when using Visual Studio?

后端 未结 25 2463
-上瘾入骨i
-上瘾入骨i 2020-11-22 14:32

I was just wondering how I could automatically increment the build (and version?) of my files using Visual Studio (2005).

If I look up the properties of sa

25条回答
  •  伪装坚强ぢ
    2020-11-22 15:18

    Maybe, for this task, you can use code like this:

        private bool IncreaseFileVersionBuild()
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                try
                {
                    var fi = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.GetDirectories("Properties")[0].GetFiles("AssemblyInfo.cs")[0];
                    var ve = System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
                    string ol = ve.FileMajorPart.ToString() + "." + ve.FileMinorPart.ToString() + "." + ve.FileBuildPart.ToString() + "." + ve.FilePrivatePart.ToString();
                    string ne = ve.FileMajorPart.ToString() + "." + ve.FileMinorPart.ToString() + "." + (ve.FileBuildPart + 1).ToString() + "." + ve.FilePrivatePart.ToString();
                    System.IO.File.WriteAllText(fi.FullName, System.IO.File.ReadAllText(fi.FullName).Replace("[assembly: AssemblyFileVersion(\"" + ol + "\")]", "[assembly: AssemblyFileVersion(\"" + ne + "\")]"));
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            return false;
        }
    

    and call it from form loading.
    With this code you can update any part of file info in AssemblyInfo.cs (but you must use "standard" directory structure).

提交回复
热议问题