Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 发布后写入txt文件时崩溃_C#_Windows_Filestream - Fatal编程技术网

C# 发布后写入txt文件时崩溃

C# 发布后写入txt文件时崩溃,c#,windows,filestream,C#,Windows,Filestream,我正在制作一个需要写入txt文件的应用程序,以便记住c#中的一些设置。我使用了这种类型的代码: class Preferiti { public static string[] Read() { string path = "Assets/mytxtfile.txt"; using (StreamReader sr = new StreamReader(path)) { string[] values = s

我正在制作一个需要写入txt文件的应用程序,以便记住c#中的一些设置。我使用了这种类型的代码:

class Preferiti
{
    public static string[] Read()
    {
        string path = "Assets/mytxtfile.txt";
        using (StreamReader sr = new StreamReader(path))
        {
            string[] values = sr.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            return values;
        }
    }

    public async static void Write(string mod)
    {
        string[] try = Read();
        bool write = true;
        for (var i = 0; i < try.Length; i++)
        {
            if (try[i] == mod)
            {
                write = false;
                break;
            }
        }
        if (write)
        {
            string path = "Assets/mytxtfile.txt";
            using (StreamWriter outfile = new StreamWriter(path, true))
            {
                await outfile.WriteLineAsync("" + mod);
                MessageBox.Show("Added to list");
            }
        }
        else
        {
            MessageBox.Show("It exist in the list");
        }
    }
}
class Preferiti
{
公共静态字符串[]读取()
{
字符串path=“Assets/mytxtfile.txt”;
使用(StreamReader sr=新StreamReader(路径))
{
string[]values=sr.ReadToEnd().Split(Environment.NewLine.tocharray(),StringSplitOptions.RemoveEmptyEntries);
返回值;
}
}
公共异步静态无效写入(字符串模式)
{
字符串[]try=Read();
bool write=true;
for(变量i=0;i
当我在我的设备上调试它时,它工作没有任何问题,但是如果我在存储上发布它,它读取文件时会出现任何问题,但在写入时会崩溃并退出应用程序

你能帮我解决这类错误吗

多谢各位

我张贴了你建议我改正的图片。。。为什么要这样对我


发布发布版本时,我猜您没有复制原始文件


在Visual Studio中,双击
mytxtfile.txt
打开属性窗口。在“属性”窗口中,将属性“复制到输出目录”设置为“始终复制”。

它会引发什么错误?请注意,在使用streamReader时,在尝试读取itI之前,您应该始终检查文件是否存在。我真的不知道是什么错误,因为它出现在已发布的版本上。在调试版本上,所有都可以正常工作。阅读过程中,它的工作没有问题。。。我在尝试写入txt文件时遇到问题。因此,现在请确保您在应用程序文件夹(您的
.exe
所在的位置)中有
Assets
文件夹和
mytxtfile.txt
。如果是这样的话,我会添加MsgBox来查找它失败的确切行。我认为这是因为该文件是只读的,但我找不到删除该文件的适当方法…Look in.Yep,但是在我的代码中应该包含该代码吗?这是关于File.GetAttributes(路径)的讨论,并告诉我这根本不存在!使用System.IO添加