Вопрос

I am trying to read a file not created in the application.

Here is the sample i tried :

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

I throw a "isolatedstorageexception" : link to exception

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()

Can someone help me to read this file ?

Thank you !

Это было полезно?

Решение

If you try to read a file that is included in your project, it won't be in the IsolatedStorage. You need to access it via Application.GetResourceStream.

Here is a sample code to read a local text file:

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

Don't forget to set the Build action to Content on the properties of the file in Visual Studio.

ReadTextFile("data/stops.txt")

Другие советы

If you wand to save a list of objects. You can do this:

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

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

Get the data:

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

And the Class look like this, remember to to add "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 с атрибуция
Не связан с StackOverflow
scroll top