Click here to Skip to main content
13,832,097 members
Rate this:
Please Sign up or sign in to vote.
c# How to create a button to save/open .txt file from Richtextbox tool without save/open filedialog with a custom filename written in textbox by finding/creating the files in one directory

im a beginner and sorry with my english

savebutton, so first of all i want it create a folder for all the .txt files stored,the folder called "Faktur" its ok if its already exist and i want it created in exe's directory after installation, and it saves without savefileDialog

loadbttn, and when loadbutton pressed it enter the folder Faktur and automatically find the filename that written in the txtidstruk.Text without openfileDialog

sorry if its too much to ask can someone help me please.. im appreciate all helps thanks

What I have tried:

private void tbsave_Click(object sender, EventArgs e)

        SaveFileDialog savefile = new SaveFileDialog();      
        savefile.RestoreDirectory = true;
        savefile.InitialDirectory = "e:\\faktur";
        savefile.FileName = String.Format("{0}.txt", txtidstruk.Text);
        savefile.DefaultExt = "*.txt*";
        savefile.Filter = "TEXT Files|*.txt";

        if (savefile.ShowDialog() == DialogResult.OK)
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(savefile.FileName))

private void tbOpen_Click(object sender, EventArgs e)

            OpenFileDialog openFile = new OpenFileDialog(); 
            openFile.InitialDirectory = "e:\\faktur";
            openFile.Filter = "text Files (*.txt)|*.txt|All files (*.*)|*.*";
            openFile.FileName = String.Format("{0}.txt", txtidstruk.Text);
            if (openFile.ShowDialog() == DialogResult.OK)
                rtfReceipt.LoadFile(openFile.FileName, RichTextBoxStreamType.PlainText);

Updated 15-Sep-18 22:51pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

private void tbOpen_Click(object sender, EventArgs e)
    string folder = "Faktur";
    string filename = textbox1.text;
    string openpath = folder + '\\' + filename;
    using (StreamReader sr = new StreamReader(openpath))
        // read the text

Similar code for the save.
Muhammad nur Ihsan 16-Sep-18 4:15am
can you explain that for?
what do i put inside here?
using (StreamReader sr = new StreamReader(openpath))
        // read the text
Richard MacCutchan 16-Sep-18 4:25am
You put the code that reads the contents of your file. Read the documentation for more information on file handling.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190114.1 | Last Updated 16 Sep 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100