C# 生成的.txt文件没有';我没有出现

C# 生成的.txt文件没有';我没有出现,c#,bitmap,filestream,streamwriter,C#,Bitmap,Filestream,Streamwriter,因此,我尝试生成一个txt文件,其中矩阵为0和1,其中所有边框都为0,矩阵体随机填充这两个值。它应该是一个位图,其中0是障碍物,1是寻路算法的可能节点。应该多次调用该方法,以根据用户的需要生成并在文件夹中保存为mush贴图 我创建了这个类来生成地图: public static class GenerateText { static string obstacle = "0"; static string node = "1";

因此,我尝试生成一个txt文件,其中矩阵为0和1,其中所有边框都为0,矩阵体随机填充这两个值。它应该是一个位图,其中0是障碍物,1是寻路算法的可能节点。应该多次调用该方法,以根据用户的需要生成并在文件夹中保存为mush贴图

我创建了这个类来生成地图:

public static class GenerateText
{
    static string obstacle = "0";
    static string node = "1";

    

    public static void CreateMap(int x, int y, string name)
    {
        string path = "Assets/" + name + ".txt";
        if(!File.Exists(name + ".txt"))
        {
            FileStream fs = File.Create(path);
            StreamWriter writer = new StreamWriter(fs);

            string[,] map = new string[x, y];
            for (int i = 0; i < x; ++i)
            {
                for (int h = 0; h < y; ++h)
                {
                    int randomValue = RandomGenerator.GetRandom(0, 10);
                    if (randomValue > 6 || i == 0 || h == 0 || i == x || h == y)
                    {
                        map[i, h] = obstacle;
                    }
                    else
                    {
                        map[i, h] = node;
                    }
                }
            }
            fs.Close();
            writer.WriteLine(map);
        }
       
    }
}
被调用的方法工作正常,并且已经结束,但是如果我检查解决方案,应该生成的文件就会丢失


我对这类事情不熟悉,所以这可能是一个愚蠢的问题,但有人能帮我吗?

在向StreamWriter写入之前,您正在关闭您的文件流

fs.Close();
writer.WriteLine(map);
最好在这种情况下使用语句,这样可以最大限度地减少此类问题。带括号或不带括号是您的选择

using (FileStream fs = File.Create(path))
using (StreamWriter writer = new StreamWriter(fs))

string[,] map = new string[x, y];
for (int i = 0; i < x; ++i)
{
    for (int h = 0; h < y; ++h)
    {
        int randomValue = RandomGenerator.GetRandom(0, 10);
        if (randomValue > 6 || i == 0 || h == 0 || i == x || h == y)
        {
            map[i, h] = obstacle;
        }
        else
        {
            map[i, h] = node;
        }
    }
}
writer.WriteLine(map);
使用(FileStream fs=File.Create(path))
使用(StreamWriter writer=新StreamWriter(fs))
字符串[,]map=新字符串[x,y];
对于(int i=0;i6 | | i==0 | | h==0 | | i==x | | h==y)
{
map[i,h]=障碍物;
}
其他的
{
map[i,h]=节点;
}
}
}
编剧。编剧线(地图);

1-在编写任何内容之前,您正在关闭filestream
2-您正在尝试编写数组。但您只需将数组对象发送到需要循环的文件即可

   public static class GenerateText
    {
        static string obstacle = "0";
        static string node = "1";



        public static void CreateMap(int x, int y, string name)
        {
            string path = Directory.GetCurrentDirectory() + "/" + name + ".txt";
            if (!File.Exists(name + ".txt"))//if there is a empty file with this name
            {                               //function doesnt work make sure you 
                                            //delete any empty file 
                FileStream fs = File.Create(path);
                StreamWriter writer = new StreamWriter(fs);

                string[,] map = new string[x, y];
                for (int i = 0; i < x; ++i)
                {
                    for (int h = 0; h < y; ++h)
                    {
                        int randomValue = RandomGenerator.GetRandom(0, 10);
                        if (randomValue > 6 || i == 0 || h == 0 || i == x || h == y)
                        {
                            map[i, h] = obstacle;
                        }
                        else
                        {
                            map[i, h] = node;
                        }
                    }
                }

                for (int a = 0; a < x; a++)
                {
                    for (int b = 0; b < y; b++)
                    {
                        writer.Write(map[a, b]);
                    }
                    writer.WriteLine();
                }
                writer.Close();
                fs.Close();
            }

        }
    }
公共静态类GenerateText
{
静态字符串障碍=“0”;
静态字符串node=“1”;
公共静态void CreateMap(int x,int y,字符串名)
{
字符串路径=Directory.GetCurrentDirectory()+“/”+name+“.txt”;
if(!File.Exists(name+“.txt”)//如果有同名的空文件
{//函数不起作用请确保
//删除任何空文件
FileStream fs=File.Create(路径);
StreamWriter writer=新StreamWriter(fs);
字符串[,]map=新字符串[x,y];
对于(int i=0;i6 | | i==0 | | h==0 | | i==x | | h==y)
{
map[i,h]=障碍物;
}
其他的
{
map[i,h]=节点;
}
}
}
对于(int a=0;a
如果您使用的是Visual Studio,请单击解决方案资源管理器上角的“显示所有文件”。是否检查了bin目录?无论如何,不要使用相对路径我检查了垃圾箱,它就在那里,我在另一个“资产”文件夹中查找。但是,当我打开文件时,它看起来是空的,我只能在代码未运行时打开它,否则它会崩溃,告诉我该文件是在另一个进程中打开的。我只是在你尝试之前更改了你的目录,只是修复了你说的对,添加了写入循环并更改了Close()过程,现在文件已经创建并写入
   public static class GenerateText
    {
        static string obstacle = "0";
        static string node = "1";



        public static void CreateMap(int x, int y, string name)
        {
            string path = Directory.GetCurrentDirectory() + "/" + name + ".txt";
            if (!File.Exists(name + ".txt"))//if there is a empty file with this name
            {                               //function doesnt work make sure you 
                                            //delete any empty file 
                FileStream fs = File.Create(path);
                StreamWriter writer = new StreamWriter(fs);

                string[,] map = new string[x, y];
                for (int i = 0; i < x; ++i)
                {
                    for (int h = 0; h < y; ++h)
                    {
                        int randomValue = RandomGenerator.GetRandom(0, 10);
                        if (randomValue > 6 || i == 0 || h == 0 || i == x || h == y)
                        {
                            map[i, h] = obstacle;
                        }
                        else
                        {
                            map[i, h] = node;
                        }
                    }
                }

                for (int a = 0; a < x; a++)
                {
                    for (int b = 0; b < y; b++)
                    {
                        writer.Write(map[a, b]);
                    }
                    writer.WriteLine();
                }
                writer.Close();
                fs.Close();
            }

        }
    }