0
votes

I have to develop application for mobile device, using Visual Studio 2008. Application have to read files and write to them.

Problem I have that I don't know where are files when I run application on mobile device or even on device emulator in VS. If I run application on PC, everything works well.

Can you give me some point what to do and where to look at?

Thank you in advance!

I didn't mention, it's for Windows Mobile 6.5

P.S. tried to work with StringBuilder like:

StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader("TestFile.txt")) 
{
    String line;
    // Read and display lines from the file until the end of 
    // the file is reached.
    while ((line = sr.ReadLine()) != null) 
    {
        sb.AppendLine(line);
    }
}
string allines = sb.ToString();
1
If you're using Windows Phone 7, you need to use isolated storage: <code> using (var store = IsolatedStorageFile.GetUserStoreForApplication()) using (var stream = new IsolatedStorageFileStream("data.txt", FileMode.Create, FileAccess.Write, store)) { // Write to our file } </code>tzerb

1 Answers

2
votes

Phone application, for security reasons, have applied the concept of Sandbox, witch means that your application does not have access to any File on the system and if you want to use your sandboxed area to manipulate files or other kind of data, you need to use their API to access it.

In Windows Phone 7 world this is called IsolateStorage

Here's a nice example about how to use it with files.


My bad I didn't read the tag windows-mobile-6.5 but I'll let my WP7 answer above and will answer you below:

Windows Mobile has no drives, so you can't use drive letters, and for the location, this is normally the root of your application place (you can find that out navigating from your Explorer), but normally this is what we do:

string path = "\\test.txt";//file Loc: *start->file explorer->text.txt*
if (!File.Exists(path))
{
    using (StreamWriter sw = File.CreateText(path))
    {
        sw.WriteLine("Hello");
        sw.WriteLine("And");
        sw.WriteLine("Welcome");
    }
}
using (StreamReader sr = File.OpenText(path))
{
    string s = "";
    label1.Text = "";
    while ((s = sr.ReadLine()) != null)
    {
        label1.Text += s;
    }
}

give that a try...