-1

Есть вот такой код(2), я бы хотел при нажатии кнопки выбрать .txt, а после выполнить вот этот код (1), с указанным текстовым документом ранее.

private void button1_Click(object sender, EventArgs e)
        {

            string str = string.Empty;
            using (System.IO.StreamReader reader = System.IO.File.OpenText(@"D:\test.txt"))
            {
                str = reader.ReadToEnd();
            }
            str = str.Replace("1", "2");            

             using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:\test.txt"))
             {
                 file.Write(str);
             }
        }

using System;
using System.Windows.Forms;

namespace WindowsFormsApp12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }
    }
}
4

1 ответ 1

1

Вот так откроется диалоговое окно, и можно будет вручную выбрать файл:

OpenFileDialog OpenFileCSV = new OpenFileDialog();
if (OpenFileCSV.ShowDialog() == DialogResult.OK)
{
    //выполнить Ваш код;
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.