Search code examples
c#filedictionaryiowindows-forms-designer

How to read from file and convert this specific file text into a <string, string> dictionary


I would like to read from a .txt file and convert the text into a <string, string> dictionary. All I would like though is for the X and Y values to be stored, however, like <X, Y>.

How can I go about this with the current text file below that I have in-place?

[System.Windows.Forms.PictureBox, SizeMode: Normal {X=359,Y=154}]
[System.Windows.Forms.PictureBox, SizeMode: Normal {X=678,Y=230}]
[System.Windows.Forms.PictureBox, SizeMode: Normal {X=625,Y=171}]
[System.Windows.Forms.PictureBox, SizeMode: Normal {X=565,Y=314}]
[System.Windows.Forms.PictureBox, SizeMode: Normal {X=409,Y=262}]
[System.Windows.Forms.PictureBox, SizeMode: Normal {X=410,Y=59}]
[System.Windows.Forms.PictureBox, SizeMode: Normal {X=777,Y=151}]

Solution

  • One way is to use a regular expression:

    var s = "[System.Windows.Forms.PictureBox, SizeMode: Normal {X=359,Y=154}]";
    
    var reg = new Regex(pattern: "{X=([0-9]*),Y=([0-9]*)}");
    
    var match = reg.Match(s);
    if(match.Success)
    {
        var x = match.Groups[1].Value; // string "359" but you can use int.TrypParse to get a number
        var y = match.Groups[2].Value;
    }