|
Just goes to show how confusing the names are.
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();
}
using (var s = File.Open(path, FileMode.OpenOrCreate))
using (var w = new StreamWriter(s))
{
w.Write("ABC");
w.Flush();
}
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
|
|
|
|
|
If I had a hat, it would be "off" to you, Sir.
You have identified the critical behavior here. I am surprised that in my extensive googling on this topic, I did not see a mention of the "truncated effect;" however, that MsDN docs don't mention it ... sadly ... is not a surprise.
I make a deep "wai" (Thai gesture of respect) towards the west in your honor.
cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
BillWoodruff wrote: however, that MsDN docs don't mention it ... sadly ... is not a surprise. The documentation is quite clear about what OpenOrCreate does:
Quote: Specifies that the operating system should open a file if it exists; otherwise, a new file should be created.
|
|
|
|
|
I do appreciate your pointing to the FileMode enum values in your earlier posts ! If I hadn't been convinced I had compared the sequence of file writes rigorously, I would have spotted an extra angle-bracket Use of only one eye is a poor excuse Richard MacCutchan wrote: The documentation is quite clear Yes, but, that does not mention the "truncate" behavior.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Why would it? That mode does not truncate the file.
|
|
|
|
|
using (Stream stream1 = new FileStream(path1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
{
dcs1.WriteObject(stream1, this.TheData1);
stream1.Position = 0L;
stream1.Close();
}
Isn't this just adding to the end of any file you already have? Potentially leading to multiple root xml elements? See if putting the Position = 0 before the WriteObject works.
using (Stream stream1 = new FileStream(path1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
{
stream1.Position = 0L;
dcs1.WriteObject(stream1, this.TheData1);
stream1.Close();
}
|
|
|
|
|
Hi, Allocation of a new FileStream in a 'Using statement does result in 'Position == 0.
I do not observe that using 'OpenOrCreate results in appending to the file; afaik: to append requires 'FileMode.Append
cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Use File.Replace().
That's assuming one can see the possibility that there is more than one way to get somewhere and bothers to try.
I'd rate the problem definition on a par with "Q and A".
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.'
― Confucian Analects
|
|
|
|
|
Hi to all.
Did anyone already work on C# wrapper for SharpSphinx or PocketSphinx for converting speech to text
Thanks.
Waleed
|
|
|
|
|
Member 2281771 wrote: C# wrapper for SharpSphinx
Googling suggests that question is odd because that library is a C# library.
The problem though appears to be finding that library.
I used the following to research that and similar should work for your other part
C# SharpSphinx
Or just look at the following or find a different solution.
C# Speech to Text[^]
You can google for other solutions with the following.
C# speech to text
|
|
|
|
|
You are absolutely right, I have 2 problems :
- Where to get SharpSphinx , because i have googles it , but did nto find it. It was hosted by Codeplex, but after Codeplex shutdown, it is not there, even into archive.
- alternative solution is to get PocketSphinx to work with C# , since it is un-managed code (C++), I cannot use the dll.
|
|
|
|
|
I already provided alternatives and I just validated again that they do seem to exist.
|
|
|
|
|
Hi to all.
Did anyone already work on C# wrapper for SharpSphinx or PocketSphinx for converting speech to text
Thanks.
Waleed
|
|
|
|
|
hi
modified 19-Jun-19 0:43am.
|
|
|
|
|
And?
What have you tried?
Where are you stuck?
What help do you need?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
1. First i am trying PDF file drag and drop in the Acrobate PDF properties there is not Event for DragDrop and DragEnter.
Any opensource avaiable? in c#
|
|
|
|
|
That still tells us nothing about what you are trying to do: we have no idea what controls you are trying to use and failing with, let alone how you are trying to use them!
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I am Using AcrobatPDF in the AcrobatPDF i have to drag and drop pdf file form my desktop or any other folder. once drag and drop the pdf file in the winforms. After Clicking PDF File it comes in fullscreen.
In AcrobatPDF its possible to drag n and drop PdF document
|
|
|
|
|
This has "long day" written all over it ...
What does that have to do with C#?
What controls are you using?
What - exactly - are you trying to do with them?
Please, remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. Unless you give us information, we can't respond to it - and we have no idea what code you are trying ...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Go to ParentI am using Windows application Web browser control
1. in my winforms i have 9 webbrowser control each web browser control load all files
eg: image, pdf, word excel
2. now in my winforms webbrowser control loading image when i drag and drop image in webbrowser1 the image size is not expand to webbrowser control window. i write this code for image expand to webbrowser control.
private void webbrowser1_DocumentCompleted(object sender,WebbrowserDocumentCompletedEventArgs e)
{
var img = webBrowser1.Document.GetElementsByTagName("img").Cast<htmlelement>().FirstOrDefault();
var w = img.ClientRectangle.Width;
var h = img.ClientRectangle.Height;
img.Style = string.Format("{0}: 100%", w > h ? "Width" : "Height");
}
after this code is expanding image in webbrowser1 control and preview full screen.
now i am trying to load pdf in Webbrowser1 show error
Object reference not set to an instance of an object.
|
|
|
|
|
Member 13706320 wrote: Object reference not set to an instance of an object.
This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.
Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.
We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!
Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.
But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
If you want to add drag & drop from Explorer, then it will work the same for any file; it's up to you whether or not to do something with the event. There's plenty examples of that in the CodeProject articles.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Which Control will Work for Acrobat Pdf in properties there is not DragDrop Event and DragEnter
|
|
|
|
|
If you're looking for a control to automatically handle the Drag and Drop for you AND/OR display a PDF for you without any work on your part, in either case, there is no such thing.
|
|
|
|
|
Hi loaded multiple files in web browser
But when I load image , image size is not fitScale in webbrowser . But pdf drag in same webbrowser it fitScale window and preview full screen.
If I use image width and height in webbrowser then image image was fitScale in webbrowser window.
But I can’t load in same webbrowser window pdf
|
|
|
|