Click here to Skip to main content
15,879,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
In my application at first start I create a text file in isolated storage
C#
isoStore.CreateFile("MyApp\\Memo.txt");

and at launching I do this (memoPrices is a List<string> that keeps some prices):
C#
public static async void FillPricesFromIsoStore()
{
    using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (isoStore.FileExists("MarketApp\\Memo.txt"))
        {
            try
            {
                IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream("MarketApp\\Memo.txt", FileMode.Open, isoStore);
                using (StreamReader file = new StreamReader(fileStream))
                {
                    String input = await file.ReadLineAsync();
                    while (input != null)
                    {
                        memoPrices.Add(input);
                        input = await file.ReadLineAsync();
                    }
                    file.Close();
                }
            }
            catch(Exception e)
            {
                // I added just this because e.InnerException doesn't return anything
                System.Diagnostics.Debug.WriteLine(e.ToString());
            }
        }
    }
}

At the first start of the app I catch an exception that says that I can't use the file because it is used in other place, that place is where it is created.
This is the output of the catch block:
C#
System.IO.IsolatedStorage.IsolatedStorageException: Operation not permitted on IsolatedStorageFileStream.
   at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, IsolatedStorageFile isf)
   at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, IsolatedStorageFile isf)
   at MarketApp.ShortTimeStoring.<FillPricesFromIsoStore>d__0.MoveNext()


What I have tried:

I tried to call the method repeatedly until the file is usable.
Posted
Updated 18-Mar-16 3:29am
v4
Comments
ZurdoDev 16-Mar-16 21:03pm    
InnerException is often null but its just a property of the exception.
Sergey Alexandrovich Kryukov 16-Mar-16 21:29pm    
His previous questions indicates that, in that case, having an inner exception is very likely. Anyway, I credited your correct comment in my answer.
—SA
Ionascut Mihai 17-Mar-16 18:12pm    
I added more breakpoints in my app and I realized that the exception isn't appearing where I thought. I edited my question and I explain the problem again. Can you look again and tell me if it's ok?
Sergey Alexandrovich Kryukov 17-Mar-16 19:10pm    
Again, you did not provide a single line of code using the property "InnerException". So, what to talk about? Now, if you cannot find out where the exception is throw, simply catch it upper on stack. Do you even understand that exceptions propagate back toward top of stack to each nearest handler? Of course, it's per thread.
—SA
Ionascut Mihai 18-Mar-16 0:52am    
I done it on my computer. You told me to add at catch block Exception.ToString() or Exception.InnerException. I added them and both indicates me that the problem is in that lines of code. I didn't post it here with that changes because the inner is very explicit about the problem and where it happeneds. I added in my code what you said in catch block and post the output

The IsolatedStorageFile.CreateFile method[^] returns an IsolatedStorageFileStream. You haven't shown the relevant parts of your code, but I suspect you forgot to close / dispose that stream, which means the file is still open.

Change the code that creates the file to close the stream when you're finished with it:
C#
using (var stream = isoStore.CreateFile("MyApp\\Memo.txt"))
{
    // Initialize the file contents here...
}

You should also change the FillPricesFromIsoStore method to wrap the stream in a using block:
C#
using (var fileStream = isoStore.OpenFile("MarketApp\\Memo.txt", FileMode.Open))
using (StreamReader file = new StreamReader(fileStream))
{
    ...
}
 
Share this answer
 
Comments
Ionascut Mihai 18-Mar-16 9:54am    
Thank you very much! The code that creates the file was the problem cause I omitted to close the file. And I added using for opening file before, but I forgot to write it... sorry for this.
It has nothing to do with Visual Studio 2013 or any particular IDE. This is a fundamental feature of .NET. It probably won't be too strong saying that if you don't use it, you are going nowhere.

All you need is this:
Exception Class (System)[^],
Exception.InnerException Property (System)[^].

During debugging, you can always insert some debug code which catches your exception and tries to examine the inner exception referenced by it, and so on, recursively. Moreover, this information is added to Exception.ToString() result. Note also the comment by RyanDev. Of course, do do any programming at all, you have to clearly understand how exceptions work and how they can be used. (If not your confused state of mind revealed by your last two questions, I would not question your knowlege of this matter.) For a really short introduction, I can offer my past answers:
Does Exception in C# Constructor Cause Caller Assignment to Fail?[^],
where was stored .net exceptions in operating system[^],
Image taken from Webcam not getting saved on the server[^].

—SA
 
Share this answer
 
v2
Comments
[no name] 17-Mar-16 18:52pm    
A 5 to give a hint once more again about exception handling in .NET
Bruno
Sergey Alexandrovich Kryukov 17-Mar-16 19:06pm    
Thank you, Bruno.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900