|
My application writes and reads file on disk. It works well, but sometimes, I meet an exception that warns: access to my file is denied. Please help me solve this problem. Thank you very much.
|
|
|
|
|
Are you closing the file properly?
If you leave the file open, it will eventually get closed when the stream object is finalized by the garbage collector. This may happen at any time, and would typically show the behaviour that you are experiencing.
Preferably you would use a using block to make sure that the file is always closed properly. Example:
using (StreamWriter writer = File.CreateText(@"c:\test.txt")) {
writer.WriteLine("Hello world.");
}
The StreamWriter (and the underlying FileStream ) will automatically be closed and disposed at the closing } .
---
single minded; short sighted; long gone;
|
|
|
|
|
Thank you very much for your help
|
|
|
|
|
Under what conditions does it sometimes fail ?
e.g. file just written and closed by app, then reopened to overwrite ?
or file just written and closed by app, then trying to delete ?
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
I don't know exactly. Because with the same condition, my application runs in different ways. It works well, but after that, when I run it again, I meet exception. I don't have much experience about this.
|
|
|
|
|
Please help me more about this. My application reads content of a MP3 or WMA file to my own file (with another extension) . When I open my own file, my application create a new MP3 file which is the same as old Mp3. I can use Windows Media Player to play this new file, but sometimes WMP can not play this file. I can not understand. Please help me. Thanks in advance! Sorry for my bad English
|
|
|
|
|
Are you still getting the same error message? Have you made sure that both files are closed properly?
How do you create the new file? What does the code look like?
---
single minded; short sighted; long gone;
|
|
|
|
|
I have closed it. I read bytes of MP3 file throught an array, and then I use FileStream.Write to write to new MP3 file. It works very well, but sometimes, it cannot play new file, and I cannot understand the reason.
|
|
|
|
|
Are you still getting the same error message?
What does the code look like?
---
single minded; short sighted; long gone;
|
|
|
|
|
I run my application two times. The first time, WMP can't play the file, the message is the file is used by another application. But the second time, WMP can play it. I think may be the process of writing new file is not complete in the first time.
|
|
|
|
|
this may be bcz. u r not calling the close method of the streamreader class object (or whatever class u r using).
when u r finish with the file u must have to call the close method so that the file u r accessing is released
|
|
|
|
|
I have already closed Filestream.
|
|
|
|
|
You should paste the particular code so that we can be of better assistance.
I am fighting against the Universe...
Reference-Rick Cook
|
|
|
|
|
I'm sorry, I open and close file during my application, and i'm a newbie in C#, so my code is not clear.
|
|
|
|
|
Hi all. I've been Googling for a sample of using the Curve class in the XNA framework without much success. I'm looking to use curves for certain 2D sprite animation paths. Does anyone have a simple example of establishing a curve for use on an XNA 2D surface?
|
|
|
|
|
Have you seen MSDN's article[^] on how to make a camera follow a curve path?
|
|
|
|
|
Hi Judah. Thanks for that reply. Yes, I had read through that example a number of times before posting my question. It's really the only example of the Curve class I can find, but I struggled to understand it and how to apply the class in a 2D game. If you know of any other examples, I'd be much obliged.
|
|
|
|
|
I'm afraid I don't know of any others off-hand. I find a lot of the XNA questions in this forum go unanswered - have you tried asking this question in an XNA-specific forum, something on GameDev.net perhaps?
|
|
|
|
|
Judah Himango wrote: have you tried asking this question in an XNA-specific forum, something on GameDev.net perhaps
Thanks again, Judah. I'm new enough to XNA that I'm still trying to find good resources. I'll give GameDev.net a try.
|
|
|
|
|
Ditto, I'm a newb when it comes to XNA, but somewhat experienced in general game dev.
I was involved a bit in Managed DirectX before they deprecated all of it: there were some good MDX sites like mdxinfo.com and thezbuffer.com. I think those sites have adapted to XNA now, they might be useful. For general game development, gameDev.net and gamasutra.com are the two mainstream ones.
|
|
|
|
|
|
I asked this question last week, but with the high wheat/chaff ratio it probably got missed...
I've run into an FxCop warning that I'm having a difficult time understanding how to get around it (other than simply supressing the message).
I have a class that implements multiple interfaces. These interfaces are:
public interface IHistoryService
{
bool Supported { get; }
Document Document { get; }
}
public interface IValidationService
{
bool Supported { get; }
ReadOnlyCollection ValidationErrors { get; }
} Since the interfaces both define bool Supported { get; } , I need to implement them explicitly. It is possible that, given different internal states, the IHistoryService functionality might be supported while the IValidationService functionality isn't (or vice versa).
For the sake of simplicity, here is a much slimmed down version of my implemenation class:
public class DocumentViewer : Form, IHistoryService, IValidationService
{
bool IHistoryService.Supported
{
get { return true; }
}
bool IValidationService.Supported
{
get { return true; }
}
} When I run FxCop against this assembly, I get the following warning:CA1033 : Microsoft.Design : Make 'DocumentViewer' sealed (a breaking change if this class has previously shipped), implement the method non-explicitly, or implement a new method that exposes the functionality of 'IDocumentInformationService.get_DocumentTemplateFullName():String' and is visible to derived classes. From the description on MSDN, it tells me:Consider a base type that explicitly implements a public interface method. A type that derives from the base type can only access the inherited interface method through a reference to the current instance (this in C#) that is cast to the interface. If the derived type re-implements (explicitly) the inherited interface method, the base implementation is no longer accessible. The call through the current instance reference will invoke the derived implementation; this results in recursion and an eventual stack overflow. So...I know I could implement
public bool Supported
{
get { return true; }
} which would satisfy the warning. The problem here is that this property doesn't make sense on the DocumentViewer, so it would be completely useless (other than to satisfy the warning).
How would I implement this so it satisfies the warning, or is this really even something to worry about?
|
|
|
|
|
I'm sure this won't help, but I'll put it out there. What if you just didn't have the Supported property?
If something doesn't support that interface, then just don't implement it on the object? Then, when you cast it to the interface, it just won't cast, thereby telling you that its not supported.
Granted, this assumes you do not have run time reasons for this property, but just wanted to throw that out there as a possibility.
As an aside, if FoxCop isn't telling you something you care to listen to, then..don't
|
|
|
|
|
Andrew Rissing wrote: What if you just didn't have the Supported property?
While this would work, it doesn't actually solve my problem. I need the interface implemented because the object in question always has the ability to provide those services but, because of internal state, might not support it at that moment in time.
Andrew Rissing wrote: As an aside, if FoxCop isn't telling you something you care to listen to, then..don't
I completely agree with you on this one and am actually ignoring the warning, but I still want to understand what it is really telling me and see if there is a "right" way to implement this.
|
|
|
|
|
Scott Dorman wrote: When I run FxCop against this assembly, I get the following warning:
CA1033 : Microsoft.Design : Make 'DocumentViewer' sealed
Can't you do that?
---
single minded; short sighted; long gone;
|
|
|
|