0

我正在尝试将文件完全读取到 String 变量中。

我这样做了:

   String text;
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            using (var readStream = new IsolatedStorageFileStream("k.dat",     FileMode.Open,        store))
            using (var reader = new StreamReader(readStream))
            {
                text= reader.ReadToEnd();
            }

        textBlock1.Text = text;`

它给了我来自 IsolatedStorageException 的“不允许对 IsolatedStorageFileStream 进行操作”消息。

我究竟做错了什么?我尝试在文件名中添加一个 .txt 和 .xml 文件,但没有成功。无论如何我要把文件放在哪里?我试过了

~\Visual Studio 2010\Projects\Parsing\Parsing\k.dat

我稍后使用以下方法解析它:

XmlReader reader = XmlReader.Create(new StringReader(xmldata));
            flagLink = false;
            while (reader.Read())
            {
//and so on
4

2 回答 2

2

尝试与..

string text;
string filename="k.txt";

    using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (isolatedStorage.FileExists(fileName))
        {
            StreamReader reader = new StreamReader(new IsolatedStorageFileStream(fileName, FileMode.Open, isolatedStorage));

            text = reader.ReadToEnd();

            reader.Close();
        }

        if(!String.IsNullOrEmpty(text))
        {
             MessageBox.Show(text);
        }
    }

编辑:
如果是 xml,

try
{
    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        IsolatedStorageFileStream isoFileStream = myIsolatedStorage.OpenFile("test.xml", FileMode.Open); //you can use your filename just like above code
        using (StreamReader reader = new StreamReader(isoFileStream))
        {
            this.textbox1.Text = reader.ReadToEnd();
        }
    }
}
catch
{ }
于 2013-06-28T11:38:30.983 回答
0

这是整个方法,并且完全有效:

String sFile = "k.dat";
        IsolatedStorageFile myFile = IsolatedStorageFile.GetUserStoreForApplication();
        //myFile.DeleteFile(sFile);
        if (!myFile.FileExists(sFile))
        {
            IsolatedStorageFileStream dataFile = myFile.CreateFile(sFile);
            dataFile.Close();
        }

        var resource = Application.GetResourceStream(new Uri(@"k.dat", UriKind.Relative));

        StreamReader streamReader = new StreamReader(resource.Stream);
        string rawData = streamReader.ReadToEnd();

        return rawData;
于 2013-06-28T14:08:23.670 回答