繁体   English   中英

Xamarin StreamWriter

[英]Xamarin StreamWriter

你可以怀疑我吗?

我有一个嵌入式资源文本文件,当我尝试使用streamWriter对其进行写入时,出现以下错误:“引发了System.ArgumentException。无法写入流。”

这是代码:

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "SuperMimo2.Products.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamWriter writer = new StreamWriter(stream))
{
    writer.WriteLine(txtLines.Text);
}

你能告诉我该怎么办?

感谢您的关注!

Android和iOS软件包/捆绑包已签名且不可写。 要写入文件,必须使用操作系统提供的适当的文件系统文件夹之一。

对于iOS,请参阅此指南:

http://developer.xamarin.com/guides/ios/application_fundamentals/working_with_the_file_system/

对于Android,您可以

var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var filename = Path.Combine(path, my_file_name);

如果要在应用程序中包含文件并对其进行写入,则首先需要将其作为资产包含在应用程序包中。 然后,您需要将其复制到可写文件夹,以便对其进行任何更改。

// read the data from the Asset
using (StreamReader sr = new StreamReader (Assets.Open ("read_asset.txt")))
    {
        content = sr.ReadToEnd ();
    }

// get a writable file path
var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var filename = Path.Combine(path, "write_asset.txt");

// write the data to the writable path - now you can read and write it
File.WriteAllText(filename, content);

暂无
暂无

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

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