C# 写入项目文件夹中的文件-不允许操作
我使用Json文件来存储与应用程序相关的对象。 阅读是有效的,但我对写文件感到困惑C# 写入项目文件夹中的文件-不允许操作,c#,json,windows-phone-8,isolatedstorage,C#,Json,Windows Phone 8,Isolatedstorage,我使用Json文件来存储与应用程序相关的对象。 阅读是有效的,但我对写文件感到困惑 private static void WriteFileContentsAsync(string content) { var local = IsolatedStorageFile.GetUserStoreForApplication(); //(new Uri("Common/DataModel/EventsData.json", UriKind.Relative)); var events
private static void WriteFileContentsAsync(string content)
{
var local = IsolatedStorageFile.GetUserStoreForApplication(); //(new Uri("Common/DataModel/EventsData.json", UriKind.Relative));
var eventsFile = local.OpenFile("Common/DataModel/EventsData.json", FileMode.Open);
using (StreamWriter writer = new StreamWriter(eventsFile))
{
writer.WriteAsync(content);
}
}
我无法打开位于我的项目->Common/DataModel/EventsData.json中的文件
异常告诉我该操作是不允许的
这是我从同一个文件中读取的方式:
private static string ReadFileContents()
{
var ResrouceStream = Application.GetResourceStream(new Uri("Common/DataModel/EventsData.json", UriKind.Relative));
if (ResrouceStream != null)
{
using (Stream myFileStream = ResrouceStream.Stream)
{
if (myFileStream.CanRead)
{
StreamReader myStreamReader = new StreamReader(myFileStream);
return myStreamReader.ReadToEnd();
}
}
}
return string.Empty;
}
但是myFileStream.CanWrite
为false
正确的选择是什么
编辑
我尝试了另一种方法,但它应该写入的文件没有更改,也没有出现错误:
private static void WriteFileContentsAsync(string content)
{
FileStream fs = new FileStream("Common/DataModel/EventsData.json",FileMode.Open,FileAccess.Write);
if (fs.CanWrite)
{
using (StreamWriter writer = new StreamWriter(fs))
{
writer.WriteAsync(content);
}
}
}
你就不能用:
IsolatedStorageFile isoFile;
isoFile = IsolatedStorageFile.GetUserStoreForApplication();
// Open or create a writable file.
IsolatedStorageFileStream isoStream =
new IsolatedStorageFileStream("Common/DataModel/EventsData.json",
FileMode.OpenOrCreate,
FileAccess.Write,
isoFile);
StreamWriter writer = new StreamWriter(isoStream);
这对那些“我不能是唯一”的迷因来说一定是很好的内容 我一直在检查我的项目文件夹以查看文件是否已更改,但我应该检查安装文件夹(在C:\data…)中的某个位置)。写作确实有效:/
希望这些示例和答案能帮助许多路人。您可以打开自己的流,然后使用RessourceWriter FileStream fs=new FileStream(“items.resources”,FileMode.OpenOrCreate,FileAccess.Write);IResourceWriter=新资源编写器(fs);ResourceWriter不可用,我应该包括什么吗?MSDN声明它应该包含在
系统中。参考资料
不,它给了我同样的异常操作,在IsolatedStorageFileStream上不允许操作
。但是用户有权限吗?例如,如果您在IIS内部运行,则应用程序池的用户需要写入权限…我想我必须对此进行调查。。。允许用户进入我的项目文件夹明智吗?我现在很困惑。也许你能为我提供一个好的起点?我甚至不知道Windows Phone使用IIS。我试图查找的资源位于项目内部(在电话上)。感谢您的回答,但正如您所看到的。我还不是专家。