程序将数据向右移动1

时间:2017-08-29 01:14:42

标签: c# winforms

下面的代码会在输入字符的末尾添加句点,但每次将数据向右移动1个额外空格。我怎么能回到那个空间?该程序对空间非常敏感

 private void button1_Click(object sender, EventArgs e)
    {
        string stringToReplace = textBox1.Text;
        if (stringToReplace.Length == 0)
        {
            MessageBox.Show("Please enter a valid string.");
            return;
        }
        if (stringToReplace.Length != 6)
        {
            MessageBox.Show("Please enter a valid string that is 6 characters in length.");
            return;
        }
        OpenFileDialog filedialog = new OpenFileDialog();
        DialogResult result = filedialog.ShowDialog(); // Show the dialog.
        if (result == DialogResult.OK) // Test result.
        {
            string filePath = filedialog.FileName;
            try
            {
                int counter = 0;
                List<string> lines = new List<string>();
                System.IO.StreamReader file = new System.IO.StreamReader(filePath);
                string line;
                while ((line = file.ReadLine()) != null)
                {
                    if (counter == 0 && !line.Contains(stringToReplace+ "."))
                    {
                        line = line.Replace(stringToReplace, stringToReplace + ".");
                    }
                    counter++;
                    lines.Add(line);
                }
                file.Close();
                string ext = Path.GetExtension(filePath);
                string newFilename = filePath.Replace(ext, "-new" + ext);
                System.IO.File.WriteAllLines(newFilename, lines.ToArray());
                MessageBox.Show("Process is completed.");
            }
            catch (IOException)
            {
                MessageBox.Show("We apologize but an error occured can you try again?");
            }
        }
    }

此屏幕截图显示了程序执行后文件的外观: enter image description here

以下屏幕截图显示了初始程序的执行方式:

enter image description here

正如你所看到它向一个方向移动。

0 个答案:

没有答案