簡體   English   中英

Windows Phone 7應用程序中的IsolatedStorageException

[英]IsolatedStorageException in a windows phone 7 application

我正在嘗試讀取未在應用程序中創建的文件。

這是我嘗試的示例:

string FileName = "stops.txt";
string FolderName = "data";
string FilePath = System.IO.Path.Combine(FolderName, FileName);

IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(FilePath, FileMode.Open, FileAccess.Read);
using (StreamReader reader = new StreamReader(fileStream))
{
    MessageBox.Show(reader.ReadLine());
}

我拋出了“ isolatedstorageexception”: 鏈接到異常

System.IO.IsolatedStorage.IsolatedStorageException: [IsolatedStorage_Operation_ISFS]
Arguments: 
Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=4.0.50829.0&File=mscorlib.dll&Key=IsolatedStorage_Operation_ISFS
   at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, IsolatedStorageFile isf)
   at System.IO.IsolatedStorage.IsolatedStorageFile.OpenFile(String path, FileMode mode, FileAccess access)
   at HorairesCTS.MainPage.test()
   at HorairesCTS.MainPage..ctor()

有人可以幫助我閱讀此文件嗎?

謝謝 !

如果您嘗試讀取項目中包含的文件,則該文件將不在IsolatedStorage 您需要通過Application.GetResourceStream訪問它。

這是讀取本地文本文件的示例代碼:

private string ReadTextFile(string filePath)
{
    var resourceStream = Application.GetResourceStream(new Uri(filePath, UriKind.Relative));
    Stream myFileStream = resourceStream.Stream;
    StreamReader myStreamReader = new StreamReader(myFileStream);
    return myStreamReader.ReadToEnd();
}

不要忘記在Visual Studio中將“ Build action設置為“ Content ”。

ReadTextFile("data/stops.txt")

如果要用魔杖保存對象列表。 你可以這樣做:

IsolatedStorageFileStream outStream = new IsolatedStorageFileStream("MyData.bin", FileMode.Create, myStore);
        DataContractSerializer ser = new DataContractSerializer(typeof(List<ClsUser>));

        ser.WriteObject(outStream, valutaTyperListe);
        outStream.Close();

獲取數據:

IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
if (myStore.FileExists("MyData.bin"))
            {

                IsolatedStorageFileStream inStream = new IsolatedStorageFileStream("MyData.bin", FileMode.Open, myStore);

                DataContractSerializer Serializ = new DataContractSerializer(typeof(List<ClsUser>));
                myUserList = Serializ.ReadObject(inStream) as List<ClsUser>;
                inStream.Close();
            }

並且Class看起來像這樣,記住要添加“ using System.Runtime.Serialization;”:

[DataContract]
public class ClsUser
{
    string name;
    string lastname;

    public ClsUser(string name, string lastname)
    {
        this.name = name;
        this.lastname = lastname;
    }

     [DataMember]
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

     [DataMember]
    public string Lastname
    {
        get { return lastname; }
        set { lastname = value; }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM