1

I have a line of code that creates an IsolatedStorageFile object.

IsolatedStorageFile isoFile = IsolatedStorageFile.GetStore(
                    IsolatedStorageScope.Roaming
                    | IsolatedStorageScope.User
                    | IsolatedStorageScope.Assembly,
                    null, null);

It works great and persists data between executions as I want it to, but when I move my exe to a different folder, it doesn't receive the same storage location. I can move the exe back to the original folder, and all of the data is available again.

How can I initialize the IsolatedStoreFile so that it always gets the same storage location no matter what folder the exe is in?

Update: In the documentation for this .GetStore is states that

null lets the IsolatedStorage object choose the evidence.

Clearly, null is using the URL of the exe as the evidence.
How can I force it to use something else?

Here's the article I used to learn about this: DeveloperFusion

4castle
  • 32,613
  • 11
  • 69
  • 106

2 Answers2

1

You can store the path to the Isolated Storageg file.

With the code below, I created a file with text and then read it back. I then 'hardcoded' the path to the file into the code (for demo purposes only!).

I moved the exe and ran it. I clicked the button that assigned the hardcoded path and was able to read the file.

It is butt-ugly but it works.

string path;
private void button1_Click(object sender, EventArgs e)
{
    // Create a file in isolated storage.
    IsolatedStorageFile store = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
    IsolatedStorageFileStream stream = new IsolatedStorageFileStream("test.txt", FileMode.Create, store);

    StreamWriter writer = new StreamWriter(stream);
    writer.WriteLine("Hello");
    writer.Close();
    stream.Close();
    // Retrieve the actual path of the file using reflection.
    path = stream.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(stream).ToString();
    MessageBox.Show("Created File:" + path);
}

private void button3_Click(object sender, EventArgs e)
{
    // Hardcoded? Yech, You should store this info somewhere
    path = @"C:\Users\xxxxxx\AppData\Local\IsolatedStorage\xzzzo1dc.wni\4xhaqngq.5jl\Url.snvxh15wown3vm2muv25oq55wafnfhxw\AssemFiles\test.txt";
}


private void button2_Click(object sender, EventArgs e)
{
    String Text = File.ReadAllText(path);

    MessageBox.Show("read storage: " + Text);
}
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
  • Thanks for your help! This could work, but how can I store the path when I can't use Isolated Storage? – 4castle Apr 04 '16 at 15:50
-2

Create a shortcut to the exe and move that around instead.

4castle
  • 32,613
  • 11
  • 69
  • 106
  • A friend of mine recommended this, so I'm including it here so that others can see it. It's not the solution I want though. – 4castle Apr 04 '16 at 15:55