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

Asked

Viewed 572 times

3

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

  1. First the user creates a subfolder to go saving the projects that were calculated by the program, as image and code below.

  2. When the user creates a subfolder, for example "Em01_project of a block 4", it is created within a predefined directory (c: Vandro Tad "Em01_project of a block 4")

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

inserir a descrição da imagem aqui

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tad_Bloco1._0
{
     public partial class NovoBloco : 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;
    }
  }
}

inserir a descrição da imagem aqui

  • 2

    Explain it better, at least for me the question is not making sense.

  • Good evening Daniel How do I create a code to save a file where the name is already fixed in the code written in C# "Loads.txt" where when the user clicks on the "Save" button, this command recognizes the path of the folder opened or created by the end user as explained in item 2.

  • In the method button_Ok_Click you save the last folder creates in the listBox_ListaBlocos, right? If so, the last record of this ListBox is the last folder created: listBox_ListaBlocos.Items[listBox_ListaBlocos.Items.Count - 1];

  • That’s right Daniel

  • Was that the only problem? Has your problem been solved then? The save part txt you got?

  • Sorry Daniel actually the listbox is just for the user to view which and how many folders he has. The last folder is in the textBox_NomeBloco

  • In fact textBox_NomeBloco.Text has the folder selected in ListBox, no? Anyway, if your problem has already been solved, delete the question, because it is not very good.

  • 1

    Daniel I rephrased the question, maybe I believe it should be clearer!

  • Really, I could understand well what your program should do, but now you are not speaking your question. What is your question? How hard are you to do what you listed up there?

  • The code upstairs is okay. My question is how to create a code in the second Forms (the second image) where when I click the "save" button, this recognizes the subfolder that was created by the user in the textBox_NomeBloco.Text and saves the fixed file "Load.txt".

  • 1

    Honestly I think you don’t have a doubt, you want someone here to do your job for you. I recommend studying a little c# and Windows Forms, is full of tutorial on the internet for this: http://bfy.tw/6ICZ

  • 1

    As I said before, I am not a programmer but a civil engineer, I am only developing this program for personal use. Thank you very much!

  • 1

    @Evandromendes It’s okay that you’re not a programmer, but even so, do a better search on the subject that you have questions about,.

Show 9 more comments

1 answer

2

I find it difficult for anyone to formulate this here for you, I did not find the question very clear and it was very specific to the point where it really seems that you are asking someone for the ready code... well, I did not understand the question but I will try to highlight a few 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 save a file that is somehow contained in the Primary Form you can try using a Global variable Setting a variable to "public" let’s assume that Form Main should call Form Example by 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 half a hand but that’s it... I prefer to pass the argument straight to form, but I think this is easier for you

  • Marcus André, Falion thank you so much for your help. I’ll try to solve it another way and so if I succeed in my code I’ll put here. I probably didn’t think my question was very clear, so I think I’d better take it off and as soon as I finish the code I insert it here. Regarding my comment about me not being a professional programmer, I only responded this way because I was upset at the way our friend Daniel responded.

  • @Evandro Mendes And there colleague. Managed to solve? If yes, post the result. You said you would post...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.