2016-04-03 68 views
1

我有一行代码创建了一个IsolatedStorageFile对象。当可执行文件移动到另一个文件夹时,IsolatedStorage丢失

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

它的伟大工程和执行之间仍然存在数据我想它,但是当我提出我的exe到不同的文件夹,它不接收相同的存储位置。我可以将exe移回原始文件夹,并且所有数据都可以再次使用。

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

更新:在documentation.GetStore是指出

让IsolatedStorage对象选择的证据。

显然,null正在使用exe的URL作为证据。
我该如何强制它使用别的东西?

这是我用来了解这个的文章:DeveloperFusion

+0

你看过这篇文章吗? http://stackoverflow.com/questions/1112681/can-i-get-a-path-for-a-isolatedstorage-file-and-read-it-from-external-applicatio –

+0

@Steve我没有,但我怎么用这个? – 4castle

+0

我发布了一个答案。 –

回答

1

您可以存储路径隔离Storageg文件。

随着下面的代码,我创建了一个文本文件,然后读回来。 然后,我将代码的路径'硬编码'到代码中(仅供演示目的!)。

我移动了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); 
} 
+0

感谢您的帮助!这可以起作用,但是当我无法使用独立存储时,如何存储路径? – 4castle

+0

你应该能够把路径放在应用程序设置...或注册表... –

-1

创建一个快捷方式到exe和移动各地来代替。

+0

我的一个朋友推荐这个,所以我在这里包括它,以便其他人可以看到它。这不是我想要的解决方案。 – 4castle

相关问题