Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I writing a small file application.

I have two text boxes, "textBox1" is for entering a note for a file, this creates a .txt file.

Second one is to read from the file. I need to assign the value of TextBox1 to a variable(filename) so that it will name the text file dynamically. I may be going about this the wrong way. Any help will be greatly appreciated. Below is the code. Waiting for some reply.
private void btnWrite_Click(object sender, EventArgs e)
 {
     String filename = textbox1.Text;
      // write a line of text to the file
     TextWriter te = new StreamWriter(???);
      // close the stream
     te.Close();
 }

 private void btnRead_Click(object sender, EventArgs e)
 {
     // create reader & open file
     TextReader tr = new StreamReader(???);

     // read a line of text
     txtRead.Text = tr.ReadLine();

     // close the stream
     tr.Close();
 }
Posted
Updated 17-May-16 22:47pm
v3

You can try

C#
private void btnWrite_Click(object sender, EventArgs e)
{
//Application.StartupPath gives the path of the executable file of the application.
    String filename = string.Format("{0}\{1}.txt",Application.StartupPath, textbox1.Text);
    string textToWrite = "Assign the text to write here";
    File.WriteAllText(filename, textToWrite);

    //You can use
    //File.AppendAllText(filename, textToWrite);
    //to append the text to the end of an existing file

}
private void btnRead_Click(object sender, EventArgs e)
{
    String filename = String filename = string.Format("{0}\{1}.txt",
                Application.StartupPath, textbox1.Text);
    string textFromFile = File.ReadAllText(filename);
}
 
Share this answer
 
v2
What problem are you facing? Are you getting any error?

If you want to know how to read and write using StreamReader and StreamWriter then refer this[^]


-----------Updated--------------------
Chech this[^]
 
Share this answer
 
v2
Comments
Member 8648589 14-Mar-12 7:31am    
i want to use the textbox value that we enter to be the name of file in which the value gets stored...

private void btnWrite_Click(object sender, EventArgs e)
{
String filename = textbox1.Text;
// write a line of text to the file
TextWriter te = new StreamWriter(filename);
// close the stream
te.Close();
}


when i give code like this ..no file is created..
nagendrathecoder 14-Mar-12 7:53am    
check my updated solution,
nagendrathecoder 14-Mar-12 7:56am    
Check my updated solution. You can use File.Create() method to create a file.
Your code is refactored as:

C#
private void btnWrite_Click(object sender, EventArgs e)
 {
     String filename = textbox1.Text;
      // write a line of text to the file
     TextWriter te = new StreamWriter(???);
     using (StreamWriter writer = new StreamWriter(filename))
     {
           writer.Write("content to be written");
           // close the stream
           writer.Close();
     }
 }


C#
private void btnRead_Click(object sender, EventArgs e)
{
    // create reader & open file
    TextReader tr = new StreamReader(???);


	using (StreamReader reader = new StreamReader(filename))
	{
	        // read a line of text
	        txtRead.Text = reader.ReadLine();

           // close the stream
           reader.Close();
	}
}
 
Share this answer
 
Comments
Member 8648589 14-Mar-12 7:45am    
where the file will be created,since we are not specifying the file path...
Member 8648589 14-Mar-12 8:24am    
but can we use the textbox value to be the filename.... for ex....if i type in the textbox "hello"....i want the file to be created as hello.txt..is there any possible way i can do that....
nagendrathecoder 14-Mar-12 8:36am    
yes, just supply this value to File.Create() method after adding some directory name to filename e.g c:\hello.txt.
you can directly create the file using the following Method of file class.

System.Io.file.writealltext(filePath,Filecontent);


here filePath is going to be the full address of your file location.

i.e, if u having a folder (coding) in D: drive of u r system.
then so the filePath must be concatinated with u r filename.

if u r textbox1.value is File1.txt

then filePath=D:\coding\File1.txt

so u can give filename dynamically from U r textbox.
 
Share this answer
 
Comments
Member 8648589 14-Mar-12 8:15am    
error is cuming in the code
System.Io.file.writealltext(filePath,Filecontent);

there is no writealltext method in System.io.file
Try this

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CP_346448_How_do_I_assign_the_value_of_a_textbox_as_a_filena
{
    class Program
    {
        static void Main(string[] args)
        {
              String filename ;
            filename=Console.ReadLine();
             // write a line of text to the file
            TextWriter te = new StreamWriter(filename );
             // close the stream
            te.WriteLine("Hi");
            te.Close();

             // create reader & open file
            TextReader tr = new StreamReader(filename);
 
            // read a line of text
           Console.WriteLine( tr.ReadLine());
           
            // close the stream
            tr.Close();
            Console.ReadKey();
        }
      
    }
}
 
Share this answer
 
Comments
Member 8648589 14-Mar-12 8:03am    
where the file will be created,since we are not specifying the file path..
nagendrathecoder 14-Mar-12 8:08am    
This won't create a file. OP wants to create and write file.
Member 8648589 14-Mar-12 8:11am    
but i want the file to be created with that textbox value name...:(
nagendrathecoder 14-Mar-12 8:16am    
check the link in my updated answer. You can use File.Create method to create a file.
Member 8648589 14-Mar-12 8:23am    
but can we use the textbox value to be the filename....

for ex....if i type in the textbox "hello"....i want the file to be created as hello.txt..is there any possible way i can do that....

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