Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
i have a string filepath = "F:\first_folder\Node3_V_1.3"

i have a button named version_check

on the click of this button i want to print the message "UPGRADING Node3 to Version 1.3"(whose details are fetched from string filepath). how do i code this in C#.
please help me out.


1)the file to be fetched is in the standard format : Nodex_V_x.x, where x will be in the range 1-7.
2) the length of the variable filepath changes dynamically( depends on the path from where it is selected)
Posted
Updated 19-Sep-15 5:13am
v2

1 solution

Try a regex:
(?<FromVer>\d(\.\d)?)_V_(?<ToVer>\d(\.\d)?)$
Should do it.
If you want to restrict numbers to just 1 to 7, then replace "\d" with "[1-7]" throughout.


"what is regex? i m not aware of this . i want the code for the above mentioned problem since am very new to c#"

"Regex" is short for "Regular Expression Parser" and it is a tool for extracting "bits" of information from a string. In this case, it does some simple pattern matching and gets the info you want from the path string.

Get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.

It also generates C# code for you!
C#
public static Regex findVersions = new Regex("(?<FromVer>[1-7](\\.\\d)?)_V_(?<ToVer>\\d(\\.\\d)?)$");
...
   Match m = findVersions.Match(filepath);
   if (m.Success)
      {
      string fromVer = m.Groups["FromVer"].Value;
      string toVer = m.Groups["ToVer"].Value;
      ...
      }
 
Share this answer
 
v2
Comments
Member 11961673 19-Sep-15 11:38am    
what is regex? i m not aware of this . i want the code for the above mentioned problem since am very new to c#
OriginalGriff 19-Sep-15 11:59am    
Answer updated.
[no name] 19-Sep-15 12:29pm    
Vote updated with 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900