473,378 Members | 1,162 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,378 software developers and data experts.

File.Create()

I have a quick question about using File.Create().

I have this code:

FileStream cacheFile = File.Create(fileName);
StreamWriter strmWriter = new StreamWriter(cacheFile);
strmWriter.Write(stringToWrite);
strmWriter.Close();
cacheFile.Close();

I'm a lazy mofo, and I'm always looking to cut as many lines as possible. I
am a self-taught programmer, so I am not too familiar what is the proper way
things are done. Would the following lines be a suitable replacement for
the above code? If not, please tell me why =)

StreamWriter strmWriter = new StreamWriter(File.Create(fileName));
strmWriter.Write(stringToWrite);
strmWriter.Close();
Thanks a bunch.
Nov 17 '05 #1
1 1907
Jeremy,

The easiest way to do this would be to do the following:

// Create the file.
using (StreamWriter strmWriter = new StreamWriter(fileName))
{
// Write the line.
strmWriter.Write(stringToWrite);
}

If you pass a string to the constructor of the StreamWriter, then it
will create the file for you if it doesn't exist (or overwrite it if it
does), using the default encoding. Also, placing it in the using statement
will cause the stream writer to be disposed of properly (as well as the
underlying stream).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jeremy McPeak" <jw******@gmail.com> wrote in message
news:OG**************@TK2MSFTNGP15.phx.gbl...
I have a quick question about using File.Create().

I have this code:

FileStream cacheFile = File.Create(fileName);
StreamWriter strmWriter = new StreamWriter(cacheFile);
strmWriter.Write(stringToWrite);
strmWriter.Close();
cacheFile.Close();

I'm a lazy mofo, and I'm always looking to cut as many lines as possible.
I am a self-taught programmer, so I am not too familiar what is the proper
way things are done. Would the following lines be a suitable replacement
for the above code? If not, please tell me why =)

StreamWriter strmWriter = new StreamWriter(File.Create(fileName));
strmWriter.Write(stringToWrite);
strmWriter.Close();
Thanks a bunch.

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Frank Millman | last post by:
Hi all I need to generate potentially large reports from a database, and I want to offer the option of print preview before actually printing (using wxPython). I figure that the best way to...
6
by: Null Reference | last post by:
Anybody here who can explain or point me to a link ? I wish to create a blank MS Access DB file programmatically using C# . Thanks, nfs
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
0
by: troutbum | last post by:
I am experiencing problems when one user has a document open through a share pointing to the web site. I use the dsolefile to read the contents of a particular directory and then display them in a...
1
by: lactaseman | last post by:
While I know this is not the correct venue... I realize this is of little to no importance to most out there... however, if I had found this in my initial searches, I would have used this. So, as...
2
by: Miro | last post by:
I will ask the question first then fumble thru trying to explain myself so i dont waste too much of your time. Question / Statement - Every mdb table needs a PrimaryKey ( or maybe an index - i...
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
65
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.