Sometimes I use FileMode.OpenOr Create 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>Certai n 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.OpenOr Create 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">Spe cifies how the operating system should write to
the file.
/// Use FileMode.OpenOr Create 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="includeCo lumnHeadings">I f true, will include column
headings.</param>
/// <returns>True if successful.</returns>
public static bool CreateDelimited File(DataTable dataTable,strin g fileName,
FileMode mode, Char delimiter, bool includeColumnHe adings)
{
FileStream fs = new FileStream(file Name,mode,FileA ccess.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 (includeColumnH eadings)
{
for (Int32 i = 0; i < dataTable.Colum ns.Count; i++)
{
line += dataTable.Colum ns[i].ColumnName + delimiter;
}
sw.WriteLine(li ne);
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.Colum ns.Count; z++) //loop throuch each
column
{
line += dataTable.Rows[i][z].ToString() + delimiter;
}
sw.WriteLine(li ne);
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">Spe cifies how the operating system should write to
the file.
/// Use FileMode.OpenOr Create 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="includeCo lumnHeadings">I f true, will include column
headings.</param>
/// <returns>True if successful.</returns>
public static bool CreateDelimited File(DataSet dataSet, string fileName,
FileMode mode, Char delimiter, bool includeColumnHe adings)
{
return
CreateDelimited File(dataSet.Ta bles[0],fileName,mode, delimiter,inclu deColumnH
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="whichTabl e">Ordinal table number to persist.</param>
/// <param name="fileName" >File to write to.</param>
/// <param name="mode">Spe cifies how the operating system should write to
the file.
/// Use FileMode.OpenOr Create 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="includeCo lumnHeadings">I f true, will include column
headings.</param>
/// <returns>True if successful.</returns>
public static bool CreateDelimited File(DataSet dataSet,Int32 whichTable,
string fileName, FileMode mode, Char delimiter, bool includeColumnHe adings)
{
return
CreateDelimited File(dataSet.Ta bles[whichTable],fileName,mode, delimiter,inclu
deColumnHeading s);
}
/// <summary>
/// Will persist named table in a DataSet to a delimited file.
/// </summary>
/// <param name="dataSet"> DataSet containing data to persist.</param>
/// <param name="whichTabl e">Named table to persist.</param>
/// <param name="fileName" >File to write to.</param>
/// <param name="mode">Spe cifies how the operating system should write to
the file.
/// Use FileMode.OpenOr Create 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="includeCo lumnHeadings">I f true, will include column
headings.</param>
/// <returns>True if successful.</returns>
public static bool CreateDelimited File(DataSet dataSet,string whichTable,
string fileName, FileMode mode, Char delimiter, bool includeColumnHe adings)
{
return
CreateDelimited File(dataSet.Ta bles[whichTable],fileName,mode, delimiter,inclu
deColumnHeading s);
}
}//end class
} 2 10824
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.OpenOr Create 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.OpenOr Create | FileMode.Trunca te
Otherwise, you should use the file mode below:
FileMode.OpenOr Create | 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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 of the old pathname,
and closing tags are left on the end resulting in an invalid XML file.
XmlSerializer.Deserialize gives a System.InvalidOperationException with
additional information: Error in the XML document.
My default file for example...
|
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 Subject or Message Body for
the e-mail that triggers the Event Sink to run.
What am I doing wrong?
Thank you,
|
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 application, while I am
doing for web. I can only find Image from web forms control and HTML control.
This may be the root cause of my problem. For read button, I converted his VB
to the C#. But the compiler complains:
|
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 errors. After reading the ole object
from db, I saved it to C: as file1.bmp and displayed on the web. But it can
not be displayed. After I manually sent the file to wordpad, it shows
|
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 leave the app & click the button,
the file is replaced.
I've tried closing and flushing, but no joy. Dispose is not an available
method. Any thoughts?
Here is a snippet (ds is a dataset):
| |
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
sample:
Dim fs1 As New System.IO.FileStream("C:\Test.txt", IO.FileMode.OpenOrCreate,
IO.FileAccess.ReadWrite, IO.FileShare.Read)
'...
'This is allowed.
|
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 the stream and writes it
out to the FileStream. Now, in the D_File function, I
read the encrypted file into a FileStream, then decrypt
it into a MemoryStream, which I pass back to my DataSet
and Read the XML into the DataSet. The problem I'm...
|
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.
I'm using AES for it is quick, and RSA to encrypt AES, to transmit AES
keys (it'll run on two separate computers).
Please notice that
- this is my first C# code :)
|
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 invalid and cannot be removed" error raised when
closing the cryptostream (or FlushFinalBlock-ing it).
For what I have read, Padding errors are due to an incorrect padding :
PKCS7 is recommended.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |