Windows Phone database and Silverlight local file operations

Source: Internet
Author: User
Preface

Recently, after debugging C # Sqlite for Windows Phone 7, I learned about the local file operations of Silverlight and recorded my ideas.

 

Isolated Storage (independent Storage space)

Isolated Storage is a separate virtual Storage space allocated to individual users. It is stored in \ % AppData % \ LocalLow \ Microsoft \ Silverlight \ is in Windows, mac OS X is stored in/Users/<user>/Library/Application Support/Microsoft/Silverlight/is.

Isolated Storage is a bit like cookies, which are stored independently by each user. The capacity of Isolated Storage is limited, but the capacity can be increased by calling System. IO. IsolatedStorage. IsolatedStorageFile. IncreaseQuotaTo.

The address of Isolated Storage.

 

 

No matter the Browser version Of Silverlight or Out Of Browser, You can freely use Isolated Storage

IsolatedStorageFile theStore = IsolatedStorageFile.GetUserStoreForApplication();
FileStream fs = new System.IO.IsolatedStorage.IsolatedStorageFileStream(@"wp.db", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, theStore);

You can add, modify, and delete files and directories in the Isolated Storage.

theStore.CreateDirectory("db");

However, files outside Isolated Storage cannot be copied to Isolated Storage. This has brought a lot of difficulties for development. For example, I want to store the sqlite database file in XAP and publish it together, and then store the data file in Isolated Storage, in the future, you can continue to modify the database and save the data in the Isolated Storage. However, XAP files cannot be copied to Isolated Storage, and pre-defined data cannot be used.

An error occurred while copying data from my documents to Isolated Storage.

 

Use my documents in Out of Browser

If the Out of Browser is used, the program can use the My Documents file.

 

Select "Enable running application out of browser" in the project properties, click "Out-of-Browser Settings", and select "Required elevated trust when running outside the browser"

 

 

if (App.Current.HasElevatedPermissions)
{
FileStream stream = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\wp.db", FileMode.OpenOrCreate);
string streamobject = new StreamReader(stream).ReadToEnd();
}

After the configuration is complete, you can use the files in my documents. If the file of my document is used, App. Current. HasElevatedPermissions must be true.

 

Use embedded resource files

The so-called embedded resource file is to package the offline file used by the program into the XAP package. You can refer to how Silverlight embeds resources for Windows Phone

 

XDocument xDoc = XDocument.Load(@"db/wp.xml");

The program can read xml files.

 

Image image = new Image();
image.Source = new BitmapImage(new Uri("Images/" + station.Image, UriKind.Relative));

You can also use image files.

 

However, you cannot open a file.

 

SaveFileDialog

SaveFileDialog provides users with the possibility of saving files to other directories, but it is restrictive and must be operated by users. Files cannot be directly saved to other locations through programs.

SaveFileDialog textDialog;
public MainPage()
{
InitializeComponent();
textDialog = new SaveFileDialog();
textDialog.Filter = "Text Files | *.txt";
textDialog.DefaultExt = "txt";
}

private void button1_Click(object sender, RoutedEventArgs e)
{
bool? result = textDialog.ShowDialog();
if (result == true)
{
System.IO.Stream fileStream = textDialog.OpenFile();
System.IO.StreamWriter sw = new System.IO.StreamWriter(fileStream);
sw.WriteLine("Writing some text in the file.");
sw.Flush();
sw.Close();
}
}

 

Thoughts on Windows Phone Databases

 

Windows Phone does not support direct operations on physical files. The traditional Silverlight approach is followed and the virtual directory space of Isolated Storage is used. I think Isolated Storage is a good practice for Silverlight. programs cannot directly operate physical files, which effectively prevents virus propagation. However, Windows Phone is a PC from the perspective of the file system. If a local PC program (such as Winform and WPF) cannot operate physical files, it would be too good. If this problem persists, a third-party database of Windows Phone will never break through because it cannot read pre-defined data.

 

Currently, there are two solutions: 1. Wait for Microsoft to issue SQL CE for Windows Phone. We can't do it. It doesn't mean that Microsoft cannot do it. Microsoft can write native C ++. In theory, everything can be done. 2. Using Cloud and network storage, I did not expect that Microsoft was far more advanced than google, and everything was cloud-based. I previously thought that Microsoft focuses on desktop systems, because most of the revenue comes from Windows and Office, while google focuses on Web. because it does not have its own operating system, it wants to use Web to control the world. However, Microsoft has also invested a lot in the Web and cloud, as can be seen from the release of HTML5 support for IE9 by Microsoft. However, for Windows Phone, it is better to provide local support because the network connectivity of mobile devices is not as good as that of other computer devices, and offline applications also have a large market.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.