Hi,

I'm trying to get my program to write the content of textbox1 into a textfile specified at a particular location. However I get an error when trying to do so.

My code:

protected void btnSave_Click(object sender, EventArgs e)
        {
           
            //Write to fileupload location
            System.IO.StreamWriter StreamWriter1 = new System.IO.StreamWriter(FileUpload1.PostedFile.FileName);

            //Writing text to the file.
           StreamWriter1.Write(TextBox1.Text);

            //Close the file.
           StreamWriter1.Close();

        }

Now i've checked the FileUpload1.Postfiled.Filename and it shows as text.txt - rather than the location clearly entered into FileUpload1 which is "C:\\Users\\Name\\Desktop\\test.txt".

If i specify the location it works perfectly, however I want user to first select the file and then save.

Thank you kindly.

>I'm trying to get my program to write the content of textbox1 into a textfile specified at a particular location.

You can not save file outside your web application. In fact , web app can not perform read / write action on client machine.

I think you misunderstood FileUpload control. FileUpload control is used to read user input in form of byte array(binary data).

However, you may create/open data file in the context of your web application.

protected void btnSave_Click(object sender, EventArgs e)
        {
           
            //Write to fileupload location
            System.IO.StreamWriter StreamWriter1 = new System.IO.StreamWriter(MapPath("~/file.txt"));

            //Writing text to the file.
           StreamWriter1.Write(TextBox1.Text);

            //Close the file.
           StreamWriter1.Close();

        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.