文件被拒绝访问
本文关键字:拒绝访问 文件 | 更新日期: 2023-09-27 18:18:22
我在写android应用,需要将json写入。txt文件。.txt文件位于项目根目录
解析json如下
string url2 = "http://new.murakami.ua/?mkapi=getProducts";
JsonValue json = await FetchAsync(url2);
private async Task<JsonValue> FetchAsync(string url)
{
// Create an HTTP web request using the URL:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
request.ContentType = "application/json";
request.Method = "GET";
// Send the request to the server and wait for the response:
using (WebResponse response = await request.GetResponseAsync())
{
// Get a stream representation of the HTTP web response:
using (Stream stream = response.GetResponseStream())
{
// Use this stream to build a JSON document object:
JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
//dynamic data = JObject.Parse(jsonDoc[15].ToString);
Console.Out.WriteLine("Response: {0}", jsonDoc.ToString());
// Return the JSON document:
return jsonDoc;
}
我试着用这个:
StreamWriter sw = new StreamWriter(@"'myfile.txt");
sw.Write(json.ToString());
sw.Close();
但是上面写着System.UnauthorizedAccessException: Access to the path "/'myfile.txt" is denied.
如何解析json文件?
尝试将路径更改为应用程序的私有目录
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string filename = System.IO.Path.Combine(path, "myfile.txt");
using (var streamWriter = new StreamWriter(filename, true))
{
//Write your data here
}
希望能有所帮助