473,395 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Saving a string into a new file

I'm writing code to perform a rather simple task but it appears to be more difficult than it should be. I have a fileContent string, containing a long string item, which I have generated within my code and wish to save and a path variable which contains the new path I wish to save the file to.

Expand|Select|Wrap|Line Numbers
  1. /*
  2.      * Process of exporting file to save it as a single document, ensuring
  3.      * that the correct extension is used.
  4.      */ 
  5.     private void ExportToSave(string path, string fileContent)
  6.     {
  7.         HttpResponse hr = HttpContext.Current.Response;
  8.         hr.AddHeader("Content-Disposition", "attachment;filename=ProRefExport");
  9.  
  10.         StreamWriter sw = new StreamWriter(path);
  11.         HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
  12.         sw.WriteLine(string.Format("Generated by user on", DateTime.Today));
  13.         sw.Close();
  14.         htmlWriter.Close();
  15.  
  16.         File.WriteAllText(path, fileContent, System.Text.Encoding.ASCII);
  17.  
  18.         hr.ContentType = "text/html";
  19.         hr.End();
  20.  
  21.         base.Render(htmlWriter);
  22.     }
  23.  
When I debug the code, the fileContent variable contains the string in full but, once the file is downloaded and saved, however, there is no text inside it.

I tried writing the fileContent string as an array of bytes using the same StreamReader object I created but made things worse as I had file process errors.

Why might this be and what can I do to solve this problem?
Mar 6 '10 #1
3 1689
tlhintoq
3,525 Expert 2GB
Have you tried putting a breakpoint at line 7 then walk through line by line to see where you get an error?

Line 16 is a File.Write. Have you opened the file with write privileges first someplace? I don't see that.
Mar 6 '10 #2
Yes, I've tried debugging it and the text shows up but no file content is saved.
I didn't realised that properties had to be assigned to a File object, I thought File.Write literally meant make a new file.
Mar 7 '10 #3
tlhintoq
3,525 Expert 2GB
Title: Saving a string into a new file
Comment: text shows up but no file content is saved.

What does "shows up" mean? Is the text in the file? Isn't that the goal; to make a file that contains your string?

Again I suggest that you open the file for writing before you try to write to it.
http://msdn.microsoft.com/en-us/library/b9skfh7s.aspx
Mar 8 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: G-Factor | last post by:
Hi all I've just started learning about saving files. I got bit of a problem. The following code gives me an error about incompatible types. (Cannot covert from class character to char *). I...
4
by: Michael Kennedy [UB] | last post by:
Hi Everyone, I have this multithreaded C# windows forms application which does a lot of image processing. Occasionally, I get the following error: A generic error occurred in GDI+....
4
by: john bailo | last post by:
For a c# program to automatically extract information from email attachments using the Outlook 9 OM, rather than saving to a file, then reading the file, I would prefer to 'Save' the attachment to...
6
by: Vijay | last post by:
I need to generate HTML files based on the some codes. One HTML file per code. I have the link (ex:http://123.234.345.456/WebPages/GetTestData.aspx?SomeCode=25), by passing the code as parameter I...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
3
by: andy_ro | last post by:
Hi group, I have an web application where the user can upload a pdf file. This file is stored in a table, in a column of type ntext. The user can later request the content of this column, and...
4
by: Pedro Leite | last post by:
Good Afternoon. the code below is properly retreiving binary data from a database and saving it. but instead of saving at client machine is saving at the server machine. what is wrong with my...
6
by: Jeff | last post by:
Hey (and thank you for reading my post) In visual web developer 2005 express edition I've created a simple website project.. At this website I want users who register to be able to upload a...
3
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
I'm working in Visual Studio 2005 and C#. I'm working in an application that automatically saves a file to a specific directory. I've seen other applications that can detect if a file of the...
3
by: pozze | last post by:
Hi, I've just made the change from ASP to .net. I have a file (code below) that saves a user submitted file to a MS SQL 2005 database. It collects the file name, file size, file type, and lastly...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.