0

IsolatedStorageFile.FileExists(string path)有效,但StreamReader(string samePath)没有?我已经验证了两条路径是相等的。我不知道为什么 StreamReader 会爆炸

     List<ProjectObj> ret = new List<ProjectObj>();
     IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();

     if (!file.DirectoryExists("/projects/")) //trying to validate the dir exists
         return ret;

     string[] fileNames = file.GetFileNames("/projects/");

     foreach (string filename in fileNames)
     {
        if (!file.FileExists("/projects/" + filename)) //validate just one more time..
            continue;

        ProjectObj tempProj = new ProjectObj();

        //Even with the validation it still breaks right here with the bellow error
        StreamReader reader = new StreamReader("/projects/"+filename); 

mscorlib.ni.dll 中出现“System.IO.DirectoryNotFoundException”类型的异常,但未在用户代码中处理

消息:找不到路径“C:\projects\Title_939931883.txt”的一部分。

4

3 回答 3

1

试试这个。在 IsolatedStorage 中读写文件有不同的路径,应该这样使用。您应该考虑阅读如何:读取和写入独立存储中的文件

        public static List<ProjectObj> getProjectsList()
        {
            List<ProjectObj> ret = new List<ProjectObj>();
            IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();

            if (!file.DirectoryExists("/projects/")) //trying to validate the dir exists
                return ret;

            string[] fileNames = file.GetFileNames("/projects/");

            foreach (string filename in fileNames)
            {
                if (!file.FileExists("/projects/" + filename)) //validate just one more time...
                    continue;

                ProjectObj tempProj = new ProjectObj();

                using (var isoStream = new IsolatedStorageFileStream("/projects/" + filename, FileMode.Open, FileAccess.Read, FileShare.Read, file))
                {
                    using (StreamReader reader = new StreamReader(isoStream))
                    {
                    }
                }
于 2013-07-20T19:54:50.657 回答
1

两种情况下的路径都不相同。在first case你得到User store for application然后在其中搜索文件。但在later case你只是在搜索base directory

StreamReader构造函数期望absolute path文件。

您需要创建IsolatedStorageFileStream并将其传递给StreamReader-

using (IsolatedStorageFileStream fileStream =  new IsolatedStorageFileStream
                                ("/projects/" + filename, FileMode.Open, file))
{
    using (StreamReader reader = new StreamReader(fileStream ))
    {
    }
}
于 2013-07-20T19:59:00.803 回答
0

这是我想出的解决方案

        List<ProjectObj> ret = new List<ProjectObj>();
        IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();

        if (!file.DirectoryExists("/projects/"))
            return ret;
        foreach (String filename in file.GetFileNames("/projects/"))
        {
            IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("/projects/"+filename, FileMode.Open, FileAccess.Read);
            using (StreamReader reader = new StreamReader(fileStream))
            {
                String fileInfo = reader.ReadToEnd();
            }
        }

我不知道为什么我在启动应用程序时遇到了非法操作,但我知道为什么后来会发生这种情况。我猜当您尝试快速访问同一个文件时会导致错误。所以我添加了一个文件共享,并且我确保在运行之前处理其他访问。

于 2013-07-20T21:11:43.113 回答