2016-04-03 108 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

相關問題