简体   繁体   English

阅读从创建目录中的文件在isolatedstorage

[英]Reading a file from a created directory in the isolatedstorage

I am trying to read a written file in a created directory in the isolated storage.. The file is actually being created. 我试图读取隔离存储中创建目录中的书面文件。该文件实际上是在创建。 but when it is being read it there is an exception "Operation not permitted on IsolatedStorageFileStream."... 但是在读取它时会出现异常“ IsolatedStorageFileStream上不允许进行操作。” ...

using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
    if (!storage.DirectoryExists("CourseworkDirectory"))
        storage.CreateDirectory("CourseworkDirectory");

    XElement Coursework = new XElement(CourseworkID);
    XDocument  _doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), Coursework);
    IsolatedStorageFileStream location = new IsolatedStorageFileStream("CourseworkDirectory\\"+CourseworkID, System.IO.FileMode.Create, storage);
    StreamWriter file = new StreamWriter(location);  

    _doc.Save(file);//saving the XML document as the file
    file.Close();
    file.Dispose();//disposing the file
    location.Dispose();
}

Reading the file.... 正在读取文件...。

using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
    string searchpath = System.IO.Path.Combine("CourseworkDirectory", "*.*");

    foreach (string filename in storage.GetFileNames(searchpath))
    {
        XElement _xml;
        IsolatedStorageFileStream location = new IsolatedStorageFileStream(filename, System.IO.FileMode.Open, storage);

It is actually getting the file name but there is an exception at this point. 它实际上是在获取文件名,但是此时有一个例外。

You can achieve this by doing this--- 您可以通过执行以下操作来实现这一目标-

using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
   IsolatedStorageFileStream fileStream = storage.OpenFile(app.getSettingsPath,FileMode.Open, FileAccess.Read);
   using (StreamReader reader = new StreamReader(fileStream ))
   {
      string line;
      while((line = reader.ReadLine()) != null)
      {
         // do your work here
      }
   }
}

Try this, it works for me: Hope it works for you too 试试这个,它对我有用:希望它对你也有用

        String sb;

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

                sb = reader.ReadToEnd();

                reader.Close();
            }

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

If this doesn't work, then maybe your file doesn't exist. 如果这不起作用,则可能您的文件不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM