打开文件并使用其中的字符串

时间:2015-12-02 15:04:54

标签: c# winforms graph listbox picturebox

当我打开一个文件(我自己制作)时,我需要使用来自低谷文本的一些文件。我想使用文本的某些部分作为坐标来绘制图形。

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        char XgetalEen;
        char XgetalTwee;
        char YgetalEen;
        char Ygetaltwee;
        string XgetalSamen = "";
        string YgetalSamen = "";
        int coordinaatX;
        int coordinaatY;
        DialogResult lel = MessageBox.Show("Do you want to close this file?", "OPEN", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (lel == DialogResult.Yes)
        {
            Open();
            foreach(string s in Gcowde)
            {
                XgetalEen = s[5];
                XgetalTwee = s[6];
                YgetalEen = s[8];
                Ygetaltwee = s[9];
                XgetalSamen += XgetalEen + XgetalTwee;
                YgetalSamen += YgetalEen + Ygetaltwee;
                if(XgetalTwee==' ')
                {
                    XgetalSamen = "";
                    XgetalTwee = '0';
                    XgetalSamen += XgetalTwee + XgetalEen;
                    YgetalEen = s[7];
                    Ygetaltwee = s[8];
                    YgetalSamen = "";
                    YgetalSamen += YgetalEen + Ygetaltwee;
                }
                if(Ygetaltwee==' ')
                {
                    Ygetaltwee = '0';
                    YgetalSamen = "";
                    YgetalSamen += Ygetaltwee + YgetalEen;
                }
                MessageBox.Show(XgetalSamen + "  " + YgetalSamen);
                Int32.TryParse(XgetalSamen, out coordinaatX);
                Int32.TryParse(YgetalSamen, out coordinaatY);
                currentLocation.X += coordinaatX;
                currentLocation.Y += coordinaatY;
                Coord.Add(new Point(currentLocation.X, currentLocation.Y));

            }
            drawerryting();
        }
    }

    public void Open()
    {
        Gcowde.Clear();
        listBox1.Items.Clear();
        Coord.Clear();
        werkVlak.Clear(Color.Black);
        Coord.Add(new Point(pictureBox1.Width / 2, pictureBox1.Height / 2));
        drawerryting();
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            StreamReader sr = new StreamReader(openFileDialog1.FileName);
            string errything = sr.ReadToEnd();
            string charAdded = "";
            foreach (char s in errything)
            {

                if (s == '\n')
                {
                    Gcowde.Add(charAdded);
                    charAdded = "";
                }
                else
                {
                    charAdded += s;
                }
            }
            foreach (string s in Gcowde)
            {
                listBox1.Items.Add(s);
            }
            sr.Close();
        }

这是我打开文件并从字符串中获取坐标的代码。字符串属于此类L1 G2 X50 Y50。我需要从字符串中取出2 50个。

ps:变量是荷兰语。

XgetalEen = XnumberOne, XgetalTwee=XnumberTwo, 

同样适用于Y.

XgetalSamen=XnumberTogether, YgetalSamen=YnumberTogether.

1 个答案:

答案 0 :(得分:0)

这是一个如何解析文件的简单示例:

// Read your file using File.ReadAllLines
String[] lines = new[] { "L1 G2 X50 Y50", "L1 G2 X50 Y50" };
foreach (var line in lines)
{
     String[] values = line.Split(' ');
     string x = values.Where(s => s.StartsWith("X")).First().Replace("X", String.Empty);
     int xCoordinate = Convert.ToInt32(x);            
}

不要忘记添加所有必要的检查和阅读其他变量。