How to get the path of the subfolder that was "SELECTED" in the listbox by the user and save the file inside

3

The program I'm creating should take the following steps:

  • First, the user creates a subfolder to save the projects that have been calculated by the program, according to the image and code below.

  • When you create a subfolder, for example "EM01_Project of a block 4", it is created within a predefined directory (c: \ evandro \ tad \ "EM01_Project of a block 4")

  • How do I save a file where the name already set in the code written in C # "Loads.txt" where when the user clicks the "Save" button, as shown below, this command recognizes the path of the subfolder opened or created through the textBox_BootName.Text by the end user as explained in item 2?

  • usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.IO;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceTad_Bloco1._0{publicpartialclassNovoBloco:Form{string[]listapasta=Directory.GetDirectories(@"c:\evandro\tad");        
    
        public NovoBloco()
        {
            InitializeComponent();
        }
    
        private void NovoBloco_Load(object sender, EventArgs e)
        {
    
            foreach (string p in listapasta)
                listBox_ListaBlocos.Items.Add(Path.GetFileName(p));
    
        }
    
        private void button_Ok_Click(object sender, EventArgs e)
        {          
            string pasta = @"c:\evandro\Tad\" + textBox_NomeBloco.Text;
    
            if (Directory.Exists(pasta) == false)
            {
                Directory.CreateDirectory(pasta);
                listBox_ListaBlocos.Items.Add(Path.GetFileName(pasta));
                MessageBox.Show("Pasta criada com sucesso!");
            }
            else
                MessageBox.Show("Esta pasta já existe");
        }
    
        private void button_Cancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    
        private void listBox_ListaBlocos_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox_NomeBloco.Text = listBox_ListaBlocos.Text;
        }
      }
    }
    

        
    asked by anonymous 16.06.2016 / 01:54

    1 answer

    2

    I find it difficult to formulate this here for you, I did not find the question very clear and it was very specific to the point that it really seems like you're asking the code ready for someone ... well, I did not understand the question but I'll try to some points ....

    • You can write a txt with this simple command
    • System.IO.File.WriteAllText ("C: \ example.txt", "content");

    If the question is a button on a secondary Form saving a file that is somehow contained in the Primary Form you can try to use a Global variable Defining a variable as "public" let's assume that Form Main should call the Form Example passing it a string ... there is more than one way to do this, I'll try to show you the simplest ...

    public class Main : Form
    {
        public Main(){
            InitializeComponents();
        }
        public void Button1_Click(object o, EventArgs e){
            Exemplo form = new Exemplo();
            form.Caminho = "C:\Exemplo.bin";//aqui é onde a Form Exemplo salvará
            form.Conteudo = "Conteúdo de Exemplo";//aqui é o conteúdo a salvar...
            form.ShowDialog();
        }  
    
    }
    public class Exemplo : Form
    {
        public string Caminho;
        public string Conteudo;
        public Exemplo(){
            InitalizeComponents();
        }
    
        public void Salvar_Click(object o, EventArgs e){
            System.IO.File.WriteAllText(Caminho, Conteudo);
        }
    
    }
    

    Well, I ended up giving it a kissed hand but that's it ... I'd rather pass the argument straight to form, but I think this is easier for you.

        
    16.06.2016 / 08:03