Загрузка...

How to save path to string variable with FileDialog

Thread in C# created by Minin_inactive4471240 Feb 10, 2022. 142 views

  1. Minin_inactive4471240
    Minin_inactive4471240 Topic starter Feb 10, 2022 3 Sep 5, 2021

    private void button2_Click(object sender, EventArgs e)
    {


    var dialog = new OpenFileDialog();
    if (dialog.ShowDialog() != DialogResult.OK)
    {
    string path = dialog.FileName;
    MessageBox.Show(path);
    return;

    }

    }
     
  2. утин
    утин Feb 10, 2022 5 Nov 13, 2019
    [IMG] я бы сделал что-то типо такого
     
  3. mikuzeboka
    mikuzeboka Feb 11, 2022 5 Jan 13, 2021
    C#
    var fileContent = string.Empty;
    var filePath = string.Empty;

    using (OpenFileDialog openFileDialog = new OpenFileDialog())
    {
    openFileDialog.InitialDirectory = "c:\\";
    openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    openFileDialog.FilterIndex = 2;
    openFileDialog.RestoreDirectory = true;

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
    //Get the path of specified file
    filePath = openFileDialog.FileName;

    //Read the contents of the file into a stream
    var fileStream = openFileDialog.OpenFile();

    using (StreamReader reader = new StreamReader(fileStream))
    {
    fileContent = reader.ReadToEnd();
    }
    }
    }
     
    1. утин
      mikuzeboka, а нахуя ему собственно данные с txt в переменной?
Top
Loading...