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);
发生了'System.IO.DirectoryNotFoundException'类型的异常 在mscorlib.ni.dll中但未在用户代码中处理
消息:无法找到路径的一部分 'C:\项目\ Title_939931883.txt'
。
答案 0 :(得分:1)
试试这个。在IsolatedStorage中读取和写入文件有不同的路径,应该像这样使用。您应该考虑阅读How to: Read and Write to Files in Isolated Storage。
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))
{
}
}
答案 1 :(得分: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 ))
{
}
}
答案 2 :(得分: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();
}
}
我不知道为什么我在启动应用程序时遇到非法操作,但我知道为什么以后会发生这种情况。我想当你尝试快速访问同一个文件时会导致错误。所以我添加了一个文件共享,并确保在此之前处理其他访问。