【问题标题】:IsolatedStorage is lost when executable is moved to another folder将可执行文件移动到另一个文件夹时,IsolatedStorage 会丢失
【发布时间】:2016-04-03 15:09:59
【问题描述】:

我有一行代码创建了一个IsolatedStorageFile 对象。

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

它工作得很好,可以按照我的意愿在执行之间保留数据,但是当我将 exe 移动到不同的文件夹时,它不会收到相同的存储位置。我可以将exe 移回原来的文件夹,所有数据都可以重新使用了。

如何初始化IsolatedStoreFile,以便无论exe 位于哪个文件夹,它始终获得相同的存储位置?

更新:在documentation 这个.GetStore 中声明

null 让 IsolatedStorage 对象选择证据。

很明显,null 正在使用exe 的 URL 作为证据。
我怎样才能强制它使用其他东西?

这是我曾经了解过的文章:DeveloperFusion

【问题讨论】:

标签: c# .net directory exe isolatedstorage


【解决方案1】:

您可以存储独立存储文件的路径。

使用下面的代码,我创建了一个带有文本的文件,然后将其读回。 然后我将文件的路径“硬编码”到代码中(仅用于演示目的!)。

我移动了 exe 并运行了它。我单击了分配硬编码路径的按钮并能够读取文件。

虽然很丑,但是很管用。

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);
}

【讨论】:

  • 感谢您的帮助!这可以工作,但是当我不能使用独立存储时如何存储路径?
【解决方案2】:

创建exe 的快捷方式,然后移动那个

【讨论】:

  • 我的一个朋友推荐了这个,所以我把它包括在这里,以便其他人可以看到它。但这不是我想要的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多