views:

153

answers:

3

Hi guys.

I am having problem with saving of my object. Take a look at this code:

public void SerializeToXML(String FileName)
{
    XmlSerializer fSerializer = new XmlSerializer(typeof(Configuration));
    using (Stream fStream = new FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None))
    {
        fSerializer.Serialize(fStream, this);
    }
}

The problem is that when the user does not have rights to the location on hard disk, this function will not throw me any exception and do not save my file. For example saving to "C:\test.xml" act like nothing happened. And I would like to know if the file has not been saved and it would be good to know the reason why.

I know that I could check if the file on given location exists and throw an exception manualy but shouldn't this be done by the XmlSerializer or FileStream itself?

Thanks for your time

Edit:

As I was suspecting I had to turn on some additional debugging. Since I am using the using clause, the "Enable unmanaged code debugging option" must be check in project properties under the Debug section. After this, the exception is shown in the debugging process.

Edit2

Replacing the above using clause with this code triggers the exception:

public void SerializeToXML(String FileName)
{
    XmlSerializer fSerializer = new XmlSerializer(typeof(Configuration));
    Stream fStream = new FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None);
    try
    {
        fSerializer.Serialize(fStream, this);
    }
    finally
    {
        fStream.Close();
    }
}
+1  A: 

That sounds very strange to me - and it doesn't sound like it has anything to do with the serializer. If you don't have access rights to a particular location, then creating the FileStream should throw an exception; it shouldn't wait until the Serialize line.

Are you absolutely sure that you don't have some catch block higher up which is hiding the problem from you?

Jon Skeet
@Jon, I am sure. I've tested it by trying to save to a folder which doesn't exists and the DirectoryNotFoundException is thrown. Maybe I have this exception excluded somewhere in the debugger options?
Wodzu
A: 

I suspect that it has to do with your using statement, because it uses a hidden try-finally construction (where the Dispose() method is called in the finally clause). Information about exceptions which are not visible outside the using block can be found here.

Try replacing the using statement by an instantiation and a call to it's Dispose() method, and I think your problem is solved. Of course, you should afterwards enclose your code in an explicit try-finally structure as part of good programming practice.

Virtlink
+1 That was the closest to solution of my problem. Ironicaly someone gave you -1 for it.
Wodzu
They probably gave him a -1 because it's incorrect. The using statement will *never* prevent an exception from being visible outside of its scope. And the link is to an issue with a design flaw in WCF, not anything to do with streams which do not through on Close/Dispose.
Josh Einstein
But in my case it prevents exception from firing up until I check the mentioned debugger option.
Wodzu
That's not possible. I guarantee if you change your code from a using statement to an explicit try/finally block you will not see a single difference in the way the code executes (because C# will compile to the exact same IL in either case.)
Josh Einstein
I did as you say Josh and you are wrong in this case. Look at my updated answer. Please take a note that I am saying all the time about the program running **under** debugger.
Wodzu
+1  A: 

Vista file redirection can explain this. It is a feature to allow legacy programs that don't handle UAC properly to still operate. The file gets redirected to the virtual store.

This will happen when you use Visual Studio 2005 or earlier or did something to prevent the manifest from getting embedded in the exe. Fix it by including a manifest.

Hans Passant
Thank you nobugz. It was not the redirection problem. I had to enable unmanaged code debugging. After second search I must admit that, the file has been save in the virtual store location. However the trick with debugger switch stays valid.
Wodzu
Let's put that explanation up in the Hall of Great Unsolved Mysteries. I'll buy you finding it in the virtual store though.
Hans Passant