473,503 Members | 5,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FileMode.OpenOrCreate - bug?

Sometimes I use FileMode.OpenOrCreate to write data to a file. Then, when I
try to write to that file again, remanants of the first set of data exist in
the file.
I'd understand if it was trying to append or something, but when only part
of the first set of data exists, I think it's an error. If anyone has any
information on this, it would be good... For now I will just warn the users
of this class, so no resolution is necessary...
Thanks!

Code follows for those interested...
using System;

using System.Data;

using System.IO;

namespace WSDOT.Spectrum

{

/// <summary>

/// Will write data to a file, delimiting each field by a specified
delimiter.

/// </summary>

/// <remarks>Certain characters may adversely affect results. For example,
if

/// the chosen delimiter is a semi-colon, existing semi-colons in the data
itself may cause problems for

/// those applications that consume the resultant file.

/// <p>Newline characters in the data itself may cause a line break where
one might not be wanted.

/// </p>

/// <p>If the data contains columns of special data types (such as 'image'),
the string 'System.Byte[]'

/// will be written in place of the actual value.

/// </p>

/// <p>Use FileMode.OpenOrCreate with caution - its behavior is
unpredictable

/// when used against a file that already contains data.

/// </p>

/// <p>It is the programmer's responsibility to anticipate and respond to
such situations.

/// </p>

/// </remarks>

public class DelimitedWriter

{

/// <summary>

/// Constructor is private so class cannot be instantiated.

/// </summary>

private DelimitedWriter()

{

}

/// <summary>

/// Will persist given DataTable to a delimited file.

/// </summary>

/// <param name="dataTable">DataTable containing data to write.</param>

/// <param name="fileName">File to write to.</param>

/// <param name="mode">Specifies how the operating system should write to
the file.

/// Use FileMode.OpenOrCreate with caution - its behavior is unpredictable
when used against a file that already contains data.</param>

/// <param name="delimiter">Delimiter to use.</param>

/// <param name="includeColumnHeadings">If true, will include column
headings.</param>

/// <returns>True if successful.</returns>

public static bool CreateDelimitedFile(DataTable dataTable,string fileName,
FileMode mode, Char delimiter, bool includeColumnHeadings)

{

FileStream fs = new FileStream(fileName,mode,FileAccess.Write);

StreamWriter sw = new StreamWriter(fs);
//The "seek" method fixes an issue which is...

//If a file is created with

string line = "";

//Write the column headings if desired.

if (includeColumnHeadings)

{

for (Int32 i = 0; i < dataTable.Columns.Count; i++)

{

line += dataTable.Columns[i].ColumnName + delimiter;

}

sw.WriteLine(line);

line = "";

}

//Now write the actual values.

for (Int32 i = 0; i < dataTable.Rows.Count; i++) //loop through each row

{

for (Int32 z = 0; z < dataTable.Columns.Count; z++) //loop throuch each
column

{

line += dataTable.Rows[i][z].ToString() + delimiter;

}

sw.WriteLine(line);

line = "";

}

sw.Close();

fs.Close();

return true;

}

/// <summary>

/// Will persist first table in a DataSet to a delimited file.

/// </summary>

/// <param name="dataSet">DataSet containing data to persist.</param>

/// <param name="fileName">File to write to.</param>

/// <param name="mode">Specifies how the operating system should write to
the file.

/// Use FileMode.OpenOrCreate with caution - its behavior is unpredictable
when used

/// against a file that already contains data.

/// </param>

/// <param name="delimiter">Delimiter to use.</param>

/// <param name="includeColumnHeadings">If true, will include column
headings.</param>

/// <returns>True if successful.</returns>

public static bool CreateDelimitedFile(DataSet dataSet, string fileName,
FileMode mode, Char delimiter, bool includeColumnHeadings)

{

return
CreateDelimitedFile(dataSet.Tables[0],fileName,mode,delimiter,includeColumnH
eadings);

}

/// <summary>

/// Will persist ordinally-numbered table in a DataSet to a delimited file.

/// </summary>

/// <param name="dataSet">DataSet containing data to persist.</param>

/// <param name="whichTable">Ordinal table number to persist.</param>

/// <param name="fileName">File to write to.</param>

/// <param name="mode">Specifies how the operating system should write to
the file.

/// Use FileMode.OpenOrCreate with caution - it's behavior is unpredictable
when used

/// against a file that already contains data.

/// </param>

/// <param name="delimiter">Delimiter to use.</param>

/// <param name="includeColumnHeadings">If true, will include column
headings.</param>

/// <returns>True if successful.</returns>

public static bool CreateDelimitedFile(DataSet dataSet,Int32 whichTable,
string fileName, FileMode mode, Char delimiter, bool includeColumnHeadings)

{

return
CreateDelimitedFile(dataSet.Tables[whichTable],fileName,mode,delimiter,inclu
deColumnHeadings);

}

/// <summary>

/// Will persist named table in a DataSet to a delimited file.

/// </summary>

/// <param name="dataSet">DataSet containing data to persist.</param>

/// <param name="whichTable">Named table to persist.</param>

/// <param name="fileName">File to write to.</param>

/// <param name="mode">Specifies how the operating system should write to
the file.

/// Use FileMode.OpenOrCreate with caution - it's behavior is unpredictable
when used

/// against a file that already contains data.

/// </param>

/// <param name="delimiter">Delimiter to use.</param>

/// <param name="includeColumnHeadings">If true, will include column
headings.</param>

/// <returns>True if successful.</returns>

public static bool CreateDelimitedFile(DataSet dataSet,string whichTable,
string fileName, FileMode mode, Char delimiter, bool includeColumnHeadings)

{

return
CreateDelimitedFile(dataSet.Tables[whichTable],fileName,mode,delimiter,inclu
deColumnHeadings);

}

}//end class

}

Nov 13 '05 #1
2 10813
Hello Marty,

I did some research, however, I did not find any known issue about this
problem. I'd like to share the following information with you:

1. When a file already exists, FileMode.OpenOrCreate is the same with
FileMode.Open. The consequent write operation will overwrite the content of
the file from the beginning.

2. If you want to keep the existing file data, I recommend you use the
following file mode:
FileMode.OpenOrCreate | FileMode.Truncate

Otherwise, you should use the file mode below:
FileMode.OpenOrCreate | FileMode.Append

3. If the problem persists on your side, could you please tell me the
detailed steps to reproduce the problem? I will be glad to check it on my
side.

I look forward to hearing from you.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ˇ°as isˇ± with no warranties and confers no rights.
Nov 15 '05 #2
Hi Marty,

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ˇ°as isˇ± with no warranties and confers no rights.
Nov 15 '05 #3

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

Similar topics

3
4148
by: Integer Software | last post by:
Hi. I have a really simple set of classes that writes 2 pathnames to a xml file. I can write the default ok. Then if I change 1 pathname to a shorter one, then rewrite the xml file, the remains...
0
3617
by: Notlwonk | last post by:
I have written an event sink in C# that hooks into our Exchange Server 2000 store. The sink fires and the various LOG files are created, but for some reason I cannot seem to read-in the e-mail...
3
3621
by: dale zhang | last post by:
Hi, I am trying to read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp The article author is using PictureBox for windows...
4
3272
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...
7
3204
by: Jeremy | last post by:
I have a procedure that writes an xml file to disk. When I click my button and run it once, it replaces the xml file on disk as it should. If I click the button a 2nd time, it appends. If I...
9
2133
by: ljlevend | last post by:
I have two questions related to FileStreams. 1. Is there any way to determine whether a file has the permissions that are required by a FileStream constructor? For example, given the following...
3
3191
by: Anon | last post by:
I made this class to encrypt my DataSet before saving it to disk. So, first in the main program I write the DataSet to XML in a MemoryStream. I pass this stream to the E_File sub, which encrypts...
4
6199
by: floppyzedolfin | last post by:
Hello! I'm actually encoding an encryption / decryption program. The encryption programes takes a file path in parameter, and encrypts the contents of the file and stores that into another file. ...
3
5799
by: floppyzedolfin | last post by:
Hi there. I'm coding an encryption / decryption program. At this very moment, I think I should be pretty close from the end, but there's something blocking me on my way. There's a "Padding is...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7357
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...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.