C #: Use the pre-generated command of Visual Studio to obtain the SVN version number,

Source: Internet
Author: User

C #: Use the pre-generated command of Visual Studio to obtain the SVN version number,

There was a winfrom project that wanted to obtain the SVN version number as the internal compilation version number of the program. There are also various methods on the Internet, but there is no practical method. So after a series of studies, I have gained some experience and summarized it into a blog.

Method 1: Obtain the version number using the SVN command

Similarly, I added a batch file named "Version_inf.bat" in the project to generate a version number and put it in the directory of the startup Item. Write the following script in the batch file:

1    svn info>bin\Debug\SVN_Version.dll2    findstr “Revision” bin\Debug\SVN_Version.dll

This script means that the "Revision" version information is obtained through the "svn info" command to the "SVN_Version.dll" file in the Debug output directory. (This command can be used properly only when svn software is installed on the computer .)

Write similar code in the main Program entry of the Program:

1 string str_PathResult = null; 2 Microsoft. Win32.RegistryKey regKey = null; // indicates the entry-level node (Registry object?) in the Windows registry ?) 3 Microsoft. Win32.RegistryKey regSubKey = null; 4 try 5 {6 regKey = Microsoft. Win32.Registry. LocalMachine; // read HKEY_LOCAL_MACHINE Item 7 8 if (regKey! = Null) 9 {10 string keyPath = @ "SOFTWARE \ TortoiseSVN"; 11 regSubKey = regKey. openSubKey (keyPath, false); 12} 13 // obtain the SVN installation path 14 if (regSubKey! = Null) 15 {16 if (regSubKey. GetValue ("Directory ")! = Null) 17 {18 str_PathResult = regSubKey. getValue ("Directory "). toString (); 19} 20} 21 22 // if SVN installation information exists, obtain the Version 23 if (str_PathResult! = Null) 24 {25 string path = System. environment. currentDirectory; 26 // Delete the existing version information to avoid the occurrence of string 27 // if (File. exists (path + "\ SVN_Version.dll") 28 // {29 // File. delete (path + "\ SVN_Version.dll"); 30 //} 31 32 int pathNum = path. lastIndexOf ("\")-3; 33 ProcessStartInfo psi = new ProcessStartInfo (); 34 35 string pathStr = path. substring (0, pathNum) + "Version_inf.bat"; 36 // string newPathStr = (pathStr. subst Ring (0, pathStr. lastIndexOf (". ") + ". bat "; 37 38 psi. fileName = pathStr; 39 // psi. fileName = path. substring (0, pathNum) + "Version_inf.bat"; 40 psi. useShellExecute = false; 41 psi. workingDirectory = path. substring (0, pathNum ). substring (0, path. substring (0, pathNum ). length-1); 42 psi. createNoWindow = true; 43 Process. start (psi); 44} 45} 46 catch (Exception ex) 47 {48 // MessageBox. show ("SVN detection error," + ex. ToString (), "prompt message"); 49 // LogUtil. WriteException (ex, "Program. Main ()"); 50} 51 finally52 {53 if (regKey! = Null) 54 {55 regKey. Close (); 56 regKey = null; 57} 58 59 if (regSubKey! = Null) 60 {61 regSubKey. Close (); 62 regSubKey = null; 63} 64}

In this way, a new "SVN_Version.dll" file is generated during each program compilation, which records the version number of the last update record.

The version information in the "SVN_Version.dll" file is displayed on the main interface of the program. The Code is as follows:

1 AssemblyName name = Assembly. getExecutingAssembly (). getName (); 2 String version = name. version. toString (). substring (0, name. version. toString (). lastIndexOf (". "); 3 // SVN version number file storage Path 4 String svnVersionPath = System. appDomain. currentDomain. baseDirectory + "SVN_Version.dll"; 5 if (File. exists (svnVersionPath) 6 {7 // StreamReader svnSteamReader = new StreamReader (svnVersionPath); 8 string [] lines = File. readAllLines (svnVersionPath); 9 if (lines. length> 0) 10 {11 for (int I = 0; I <lines. length; I ++) 12 {13 if (lines [I]. contains ("Revision") 14 {15 String [] temps = lines [I]. split (':'); 16 if (temps. length> 1) 17 {18 version + = String. format (". {0} ", temps [1]. trim (); 19 break; 20} 21} 22} 23 tssVersion. text + = version; 24} 25 else26 {27 tssVersion. text + = "Get error"; 28} 29} 30 else31 {32 tssVersion. text + = "unknown version"; 33}

This completes obtaining the SVN version number and displaying the internal compilation number. For example:

However, the problem with this method is:

1) svn commands cannot generate a version number if the program has not changed or the latest version has not been obtained;

2) Only the version number is displayed on the main interface, but the version number of the project generated file is not changed;

3) if the svn program is not installed on the computer, an error may occur;

So I studied method 2 again.

 

Method 2: Obtain the SVN version number through the project preprocessing event

Create a template class file "AssemblyInfo. template. cs" under the Properties directory of the project, and ignore the "AssemblyInfo. cs" file from the SVN version number. Write similar code in the template file:

1 using System. Reflection; 2 using System. Runtime. CompilerServices; 3 using System. Runtime. InteropServices; 4 5 // The general information about the assembly is controlled through the following 6 // feature set. Modify these feature values to modify 7 // information associated with the Assembly. 8 [assembly: AssemblyTitle ("program name")] 9 [assembly: AssemblyDescription ("updated at: $ WCDATE $")] 10 [assembly: AssemblyConfiguration ("")] 11 [assembly: AssemblyCompany ("")] 12 [assembly: AssemblyProduct ("program name")] 13 [assembly: AssemblyCopyright ("Copyright©2013 ")] 14 [assembly: AssemblyTrademark (" ")] 15 [assembly: AssemblyCulture (" ")] 16 17 // set ComVisible to false to make the type 18 in this Assembly invisible to the COM component. If you need to access the type in this Assembly from COM, 19 // set the ComVisible attribute of this type to true. 20 [assembly: ComVisible (false)] 21 22 // if this project is made public to COM, the following GUID is used for ID23 of the Type Library [assembly: Guid ("18501865-f051-43be-ab03-59a2d9e76fcf")] 24 25 // The Assembly version information consists of the following four values: 26 // 27 // main version 28 // minor version 29 // internal version 30 // Revision No. 31 // 32 // all these values can be specified, you can also use the default values of "internal version number" and "revision number". 33 // use "*": 34 // [assembly: AssemblyVersion ("1. 0. * ")] 35 [assembly: AssemblyVersion (" 1.1.1. $ WCREV $ ")] 36 [assembly: AssemblyFileVersion (" 1.1.1. $ WCREV $ ")]

Then, write the following command to run the pre-generated event in the project property generation event:

1 $(SolutionDir)Lib\SubWCRev.exe $(SolutionDir) $(ProjectDir)Properties\AssemblyInfo.template.cs $(ProjectDir)Properties\AssemblyInfo.cs -f

This example finds the subwcrev.exe file of svn, obtains the version information, and writes the data to the "AssemblyInfo. cs" file through the template.

In this way, the version number is written into the project output file after each generation. Add a template and a pre-generated event for each project according to the preceding method, and the program file will carry the version information.

Shows the actual effect:

When the program is displayed, it can encapsulate a public attribute so that others can call the version number information:

1 /// <summary> 2 /// Version 3 /// </summary> 4 public static string AppVersion5 {6 set {AppDomain. currentDomain. setData ("AppVersion", value);} 7 get {return AppDomain. currentDomain. getData ("AppVersion") = null? "": AppDomain. CurrentDomain. GetData ("AppVersion"). ToString ();} 8}

This is a success !!!

If you want to obtain the file generated by the project or the version number attribute of a specified file, you can use the following method:

1 /// <summary> 2 /// obtain the file version 3 /// </summary> 4 /// <param name = "filePath"> complete file path </param> 5 // <returns> file version </returns> 6 public static string GetFileVersion (string filePath) 7 {8 string FileVersions = ""; 9 10 try11 {12 System. diagnostics. fileVersionInfo file1 = System. diagnostics. fileVersionInfo. getVersionInfo (filePath); 13 FileVersions = file1.FileVersion; 14 if (FileVersions! = "") 15 {16 string [] strVer = FileVersions. split ('. '); 17 if (strVer. length = 2) 18 {19 FileVersions = strVer [0] + ". 00.0000 "; 20} 21 22} 23} 24 catch (Exception ex) 25 {26 FileVersions =" "; 27} 28 return FileVersions; 29}

Note: This article is my original Healer007! Signature: the source of the radish reprinted. Thank you!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.