locked
IsolatedStorageFileStream readStream = appStorage.OpenFile(File, FileMode.Open, FileAccess.Read); RRS feed

  • Question

  • WP8 with C#:

     IsolatedStorageFileStream readStream = appStorage.OpenFile(File, FileMode.Open, FileAccess.Read);

    File="eeg\2013.txt"

    When I use ISE to copy the file 2013.txt into Iso,the app can run OK.When using app to create the file 2013.txt into Iso,the app give below Exception.

    SafeFileHandle

    +  base {System.IO.IsolatedStorage.IsolatedStorageException: Operation not permitted on IsolatedStorageFileStream.
       at System.IO.IsolatedStorage.IsolatedStorageFileStream.NotPermittedError()
       at System.IO.IsolatedStorage.IsolatedStorageFileStream.get_SafeFileHandle()} System.Exception {System.IO.IsolatedStorage.IsolatedStorageException}



    Saturday, June 1, 2013 2:46 PM

Answers

  • I am glad to tell all,the trouble is solved.UploadAsync method "myFile" is only name of file,but not path,it is a mistake for me a few days.

           we all attributed the trouble to the unclose file and IsolatedStorageFileStream,it is a direction mistake. microsoft should tell the filename/path clearly;

    • Marked as answer by yang2013yang Tuesday, June 4, 2013 1:57 PM
    Tuesday, June 4, 2013 1:56 PM

All replies

  • Use the constructor directly

    IsolatedStorageFileStream isf = new IsolatedStorageFileStream("MyTextFile.txt", FileMode.OpenOrCrate, FIleAccess.ReadWrite, FileShare.ReadWrite, IsolatedStorageFile.getUserStoreForApplication());

    The above file stream opens a file(or creates it when it does not exist), in read-write mode, allowing share for other streams. All you have to do from now on is change the path from MyTextFile to whatever you need to.

    If you want to override the file every time you open the stream, replace FileMode.OpenOrCrate with FileMode.Create.

    Sunday, June 2, 2013 9:44 AM
  • I use your method,the Exception still appears.I am collapsed.

    Thanks a lot. hope having another method.

    Sunday, June 2, 2013 11:44 AM
  • You opened a filestream with FileMode.Create option, and it didn't work? Can you show the code you are using in that method?
    Sunday, June 2, 2013 5:21 PM
  • I use FileMode.OpenOrCreate, FIleAccess.ReadWrite, FileShare.ReadWrite,I will copy the code in 4 hours later.

    I first creat a txt file at the app,close it,then use the stream open it for uploading to skydrive.so it is.   


    Monday, June 3, 2013 3:58 AM
  • frist:                   

     
                       // Create a file in a subdirectory.
                        using (IsolatedStorageFileStream subDirFile =
                            store.CreateFile(Path.Combine("MyCust_C", filename)))                        //Path.Combine("MyCust_C", filename);filename
                        {
                            //subDirFile.Close();
                            //subDirFile.Dispose();
                  
                        }
    
                        // Determine if the file exists before writing to it.
                        string filePath = Path.Combine("MyCust_C", filename);// filename;//Path.Combine("MyCust_C", filename)
    
                        if (store.FileExists(filePath))
                        {
                            try
                            {
                                using (StreamWriter sw =
                                    new StreamWriter(store.OpenFile(filePath,
                                        FileMode.Open, FileAccess.Write)))
                                {
                                    sw.WriteLine(information_box.Text);//yhj "To do list:"
                                    //sw.WriteLine("1. Buy supplies.");
                                    //sw.Close();//yhj
                                    //sw.Dispose();
                                }
                            }
                            catch (IsolatedStorageException ex)
                            {
    
                                sb.AppendLine(ex.Message);
                            }
                        }
    
              

    then:

                using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
     
                    using (IsolatedStorageFileStream readStream = new IsolatedStorageFileStream(myFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, appStorage))
                    {
                        LiveOperationResult operationResult3 = await client.UploadAsync(skyDriveFolderID, myFile, readStream, OverwriteOption.DoNotOverwrite);//client.UploadAsync((skyDriveFolderID, File , true, readStream, null);
                    
                                           
                        try
                            {
                               dynamic meResult4 = operationResult3.Result;//meResult:id;name;source
                               if (meResult4.name != null)//meResult.last_name != null
                                {
                                    infoTextBlock.Text = myFile + "upload!";
                                    BeginNextUpload();
                                }
                                else
                                {
                                    infoTextBlock.Text = "Hello, signed-in user!";
                                }
                            }
                        catch (LiveConnectException exception)
                            {
                                this.infoTextBlock.Text = "Error calling API: " + exception.Message;
                            }
                    }
                    //appStorage.Dispose();//yhj
                 }
    I use above code,the Exception still appears.

    +  base {System.IO.IsolatedStorage.IsolatedStorageException: Operation not permitted on IsolatedStorageFileStream.
       at System.IO.IsolatedStorage.IsolatedStorageFileStream.NotPermittedError()
       at System.IO.IsolatedStorage.IsolatedStorageFileStream.get_SafeFileHandle()} System.Exception {System.IO.IsolatedStorage.IsolatedStorageException}


    Monday, June 3, 2013 12:19 PM
  • You are using the filestream with FileMode.Open instead of FileMode.OpenOrCreate. The file does not exist when you try to open it, thus the exception.
    Monday, June 3, 2013 1:07 PM
  • using (IsolatedStorageFileStream readStream = new IsolatedStorageFileStream(myFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, appStorage))
                    {

    also not OK;

    I use ISE to see myFile,it has been exist.


    Monday, June 3, 2013 1:18 PM
  • Does the subdirectory exist?

    Where do you create the 'MyCust_C' directory?


    Darin R.

    Monday, June 3, 2013 8:37 PM
  • 'MyCust_C' directory has been created before the "first".I use ISE to see myFile,it has been exist in \MyCust_C directory.

    I begin to think another way;Below is the code,but don`t know how to set the "myFile" string,myFile file is here:   Isolatedstore\local\MyCust_C\xxx.txt

    var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                Windows.Storage.StorageFile storageFile = await localFolder.GetFileAsync(myFile);//"CaptainsLog.store"
                System.IO.Stream mm = await storageFile.OpenStreamForReadAsync();
                 using (var appStorage6 = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    //string zi=myFile.Substring(9);
                    //StorageFile myfile1 = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/MyCust_C/"+zi)); 
             
                    // StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                    // StorageFile storageFile = await localFolder.GetFileAsync(myFile);
                    // Open it and read the contents
                    //Stream wreadStream = await storageFile.OpenStreamForReadAsync(); 
    
                    //IsolatedStorageFileStream readStream = appStorage.OpenFile(myFile, FileMode.Open, FileAccess.Read);//"2012-12-05-2053.txt",File,myFile
                    //IsolatedStorageFileStream readStream = new IsolatedStorageFileStream(myFile, FileMode.Open,FileAccess.Read, appStorage);
                    //(myFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, appStorage)
                 //   using (FileStream readStream8 = new FileStream(myFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                    //using (IsolatedStorageFileStream readStream8 = new IsolatedStorageFileStream(myFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, appStorage6))
                    {
                        LiveOperationResult operationResult3 = await client.UploadAsync(skyDriveFolderID, myFile, mm, OverwriteOption.DoNotOverwrite);//client.UploadAsync((skyDriveFolderID, File , true, readStream, null);



    Tuesday, June 4, 2013 5:00 AM
  • I am glad to tell all,the trouble is solved.UploadAsync method "myFile" is only name of file,but not path,it is a mistake for me a few days.

           we all attributed the trouble to the unclose file and IsolatedStorageFileStream,it is a direction mistake. microsoft should tell the filename/path clearly;

    • Marked as answer by yang2013yang Tuesday, June 4, 2013 1:57 PM
    Tuesday, June 4, 2013 1:56 PM