Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

Savebutton
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))
                sw.WriteLine(rtfReceipt.Text);
        }


Loadbutton
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);

        }
Posted
Updated 15-Sep-18 21:51pm

1 solution

C#
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.
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900