Can I use normal File.CreateText (path) instead of IsolStorageFile?

Everyone seems to be suggesting that you need to use isolated storage on Windows Phone 8, but I haven't found the reason. I also used some code that I ported and the usual File.CreateText(Windows.ApplicationModel.Package.Current.InstalledLocation)

one seems to work fine.

So, in the code, everyone seems to be doing this (from developer.nokia.com) :

IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamWriter Writer = new StreamWriter(new IsolatedStorageFileStream("TestFile.txt", FileMode.OpenOrCreate, fileStorage));
Writer.WriteLine(textBox1.Text);
Writer.Close();

      

It is truly exclusively tame. I've seen too many newbies do this async

and can't figure out why. However, the above code is presented in the context of WP7.


UPDATE: Although the code below worked on WP8 (HTC 8XT) and WP8.1 (Lumia 640) when launched from Visual Studio, when I was deployed to the repository, it exploded immediately when I tried to save the file.


The code below seems to work just as well, at least on the WP emulator, my HTC 8XT running Windows Phone 8 and my Lumia 640 running WP 8.1. The code below can be seen in a little improved context at this link , but it's important stuff. Yes, I use some Hungarian. I'm sorry. Obviously, your page should have a TextBox with a name txtText

and a global name strFileLoc

.
Windows.ApplicationModel.Package package =
    Windows.ApplicationModel.Package.Current;
Windows.Storage.StorageFolder installedLocation = 
    package.InstalledLocation;
this.strFileLoc = Path.Combine(installedLocation.Path, 
    "myFile.txt");

string strToWrite = this.txtText.Text;
using (StreamWriter sw = File.CreateText(this.strFileLoc))
{
    sw.WriteLine(strToWrite);
    sw.Close();
}

// Load
string strText = string.Empty;
if (File.Exists(this.strFileLoc))
{
    using (StreamReader sr = 
        new StreamReader(File.OpenRead(this.strFileLoc)))
    {
        strText = sr.ReadToEnd();
    }
}
else
{
    strText = "File doesn't exist";
}
this.txtText.Text = strText;

      

Can this be used in a production application? Why or why not?

+3


source to share


3 answers


The code works while debugging as a VS based deployment gives your application the ability to write to your installation location (annoying bug / design issue). When your app is deployed from the Repository, it does not have permissions to install it and will crash. The solution is to not try to create (or write) files in the installation folder; use one of the ApplicationData

folders
instead .

Regarding using synchronous asynchronous methods, there are two answers. The first answer is that, assuming you are making calls from the UI thread, asynchronous methods allow your UI to remain responsive even if I / O is taking a long time (as might be the case when booting from an SD card, for example) ... Relying on synchronous APIs your UI may crash or appear to crash.

The second answer is that APIs are System.IO

not valid for Universal Apps on Windows 8 / 8.1, so if you wanted to reuse code, you had no choice but to use the ...Async

WinRT API.



Starting with Windows 10 Universal Apps, you can use it System.IO.File

again across all Windows device families. And since you can set the current directory, you can do something like this:

Directory.SetCurrentDirectory(ApplicationData.Current.LocalFolder.Path);
using (var f = File.CreateText("hello.txt"))
{
  f.WriteLine("Hello, world");
}

      

Note that the current directory is a system-wide parameter , so avoid this code altogether (setting it on different threads to different values ​​will only lead to tears), but this is useful if you have existing code that relies on relative paths. Also note that ideally you could only run code like the one above on a background thread due to the fact that it will take some time to complete.

+1


source


If I recall correctly, the files are left alone during the update to the Windows Store. Regarding your other question, it is a question of the platforms you want to configure (for example most things related to development for WP7-8.1 / WIN8 / WinRT) ... refer to this MSDN forum page

Storage versus Isolated Storage




Copied from the MSDN forum

Ah, ApplicationData vis IsolatedStorage. Okay, to be honest, there isn't much of a performance difference between the two. This is more about application development options. If you are writing WP7.1 and 8 you MUST use IsolStorage as ApplicationData.LocalFolder is not available in 7.x. If you want to write code that is available in both Win8 / WinRT and WinPhone, then you must use ApplicationData, since Isolitoror is not supported on Windows 8. Both APIs are completely secure and the data can only be accessed from the created application.

So, it all depends on where you want to use your code. I suggest you use ApplicationData if you are starting from scratch and don't care about WP7.x as this is the direction that all MS OSs are headed.

If this is an answer, please mark it as an answer.

Cheers, Mark B Schramm

+1


source


I used to work on Windows Phone platform and in particular I owned the quality of the device update. User data, files, and settings are persisted in an updated process, be it AppData or Isolated Storage.

0


source







All Articles