Click here to Skip to main content
14,272,574 members
   

C#

 
GeneralRe: some bugs don't retire II: DataContract.ReadObject fails after multiple writes to the same file Pin
Richard MacCutchan18-Jun-19 21:18
protectorRichard MacCutchan18-Jun-19 21:18 
GeneralRe: some bugs don't retire II: DataContract.ReadObject fails after multiple writes to the same file Pin
BillWoodruff18-Jun-19 21:54
mveBillWoodruff18-Jun-19 21:54 
GeneralRe: some bugs don't retire II: DataContract.ReadObject fails after multiple writes to the same file Pin
Richard MacCutchan18-Jun-19 21:58
protectorRichard MacCutchan18-Jun-19 21:58 
GeneralRe: some bugs don't retire II: DataContract.ReadObject fails after multiple writes to the same file Pin
BillWoodruff18-Jun-19 22:04
mveBillWoodruff18-Jun-19 22:04 
GeneralRe: some bugs don't retire II: DataContract.ReadObject fails after multiple writes to the same file Pin
Richard MacCutchan18-Jun-19 22:39
protectorRichard MacCutchan18-Jun-19 22:39 
GeneralRe: some bugs don't retire II: DataContract.ReadObject fails after multiple writes to the same file Pin
Richard Deeming19-Jun-19 0:57
mveRichard Deeming19-Jun-19 0:57 
GeneralRe: some bugs don't retire II: DataContract.ReadObject fails after multiple writes to the same file Pin
BillWoodruff19-Jun-19 4:24
mveBillWoodruff19-Jun-19 4:24 
GeneralRe: some bugs don't retire II: DataContract.ReadObject fails after multiple writes to the same file Pin
Richard Deeming19-Jun-19 5:04
mveRichard Deeming19-Jun-19 5:04 
Just goes to show how confusing the names are. Big Grin | :-D

I've just tested it, and the problem is not that OpenOrCreate is appending to the file; it's that OpenOrCreate doesn't truncate the file.

If the new content you write to the file isn't as long as the existing content, you'll end up with the last part of the existing content left at the end. For serialization, this will obviously cause it to break.

Eg:
using (var s = File.Open(path, FileMode.OpenOrCreate))
using (var w = new StreamWriter(s))
{
    w.Write("0123456789");
    w.Flush();
}

// File now contains: 0123456789

using (var s = File.Open(path, FileMode.OpenOrCreate))
using (var w = new StreamWriter(s))
{
    w.Write("ABC");
    w.Flush();
}

// File now contains: ABC3456789

Using FileMode.Create will truncate the file if it already exists, so you won't see this problem.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: some bugs don't retire II: DataContract.ReadObject fails after multiple writes to the same file Pin
BillWoodruff19-Jun-19 12:05
mveBillWoodruff19-Jun-19 12:05 
GeneralRe: some bugs don't retire II: DataContract.ReadObject fails after multiple writes to the same file Pin
Richard MacCutchan21-Jun-19 22:01
protectorRichard MacCutchan21-Jun-19 22:01 
GeneralRe: some bugs don't retire II: DataContract.ReadObject fails after multiple writes to the same file Pin
BillWoodruff21-Jun-19 22:09
mveBillWoodruff21-Jun-19 22:09 
GeneralRe: some bugs don't retire II: DataContract.ReadObject fails after multiple writes to the same file Pin
Richard MacCutchan21-Jun-19 22:34
protectorRichard MacCutchan21-Jun-19 22:34 
AnswerRe: some bugs don't retire II: DataContract.ReadObject fails after multiple writes to the same file Pin
F-ES Sitecore18-Jun-19 23:51
mveF-ES Sitecore18-Jun-19 23:51 
GeneralRe: some bugs don't retire II: DataContract.ReadObject fails after multiple writes to the same file Pin
BillWoodruff19-Jun-19 0:54
mveBillWoodruff19-Jun-19 0:54 
AnswerRe: some bugs don't retire II: DataContract.ReadObject fails after multiple writes to the same file Pin
Gerry Schmitz19-Jun-19 5:16
mveGerry Schmitz19-Jun-19 5:16 
QuestionSharpSphinx Pin
Member 228177115-Jun-19 3:23
memberMember 228177115-Jun-19 3:23 
AnswerRe: SharpSphinx Pin
jschell15-Jun-19 3:33
memberjschell15-Jun-19 3:33 
GeneralRe: SharpSphinx Pin
Member 228177116-Jun-19 12:15
memberMember 228177116-Jun-19 12:15 
GeneralRe: SharpSphinx Pin
jschell23-Jun-19 5:45
memberjschell23-Jun-19 5:45 
QuestionUsing SharpSphinx or PocketSphinx with C# Pin
Member 228177115-Jun-19 3:14
memberMember 228177115-Jun-19 3:14 
QuestionDrag and Drop Ms-Word,Excel Pin
Member 1370632014-Jun-19 19:45
memberMember 1370632014-Jun-19 19:45 
AnswerRe: Drag and Drop Ms-Word,Excel and PowerPoint Pin
OriginalGriff14-Jun-19 19:58
protectorOriginalGriff14-Jun-19 19:58 
GeneralRe: Drag and Drop Ms-Word,Excel and PowerPoint Pin
Member 1370632014-Jun-19 21:37
memberMember 1370632014-Jun-19 21:37 
AnswerRe: Drag and Drop Ms-Word,Excel and PowerPoint Pin
OriginalGriff14-Jun-19 21:42
protectorOriginalGriff14-Jun-19 21:42 
GeneralRe: Drag and Drop Ms-Word,Excel and PowerPoint Pin
Member 1370632014-Jun-19 22:07
memberMember 1370632014-Jun-19 22:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.