Click here to Skip to main content
13,703,155 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:

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 4 days ago
Updated 4 days ago

1 solution

Rate this: bad
 
good
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.
  Permalink  
Comments
Muhammad nur Ihsan 4 days ago
   
can you explain that for?
what do i put inside here?
using (StreamReader sr = new StreamReader(openpath))
    {
        // read the text
    }
Richard MacCutchan 4 days ago
   
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
Web04-2016 | 2.8.180920.1 | Last Updated 16 Sep 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

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