无法将文本写入文件

c#

所以,基本上,我试图在我的文本 rpg 中创建一个保存点。我想通过使用 .txt 文件并以这种方式跳转到每个方法来做到这一点。但是,当我尝试输入文件的路径和字符串内容时,它返回 CS0029 错误并指出它无法将 void 转换为字符串。

public void PickingUpScroll()
    {

        savePoint = "PickingUpScroll";
        saveData = File.WriteAllText(savePath, savePoint);
         
        hasFireMagic = true;

        Console.WriteLine("You approach the strange scroll in your room and pick it up. A strange light eminates from the foreign symbols on the paper. You feel a warmth in your hands.nLooking down, you notice they are glowing with a strange light, almost like fire.");
        Console.ForegroundColor = ConsoleColor.Cyan;
        Console.WriteLine("You have accquired 'Fire Magic'");
        Console.ForegroundColor = ConsoleColor.White;


        if (hasWeapons == false)
        {
            Console.WriteLine("Do you approach the weapons?n[1] Yes [2] No");
            int scrollToSwordCheck = Convert.ToInt32(Console.ReadLine());
            if (scrollToSwordCheck == 1)
            {

                PickingUpSword();

            }
        }
        else
        {
            LeavingRoom();
        }
    }

回答

File.WriteAllText 方法是空的(它不返回任何值)

https://docs.microsoft.com/en-us/dotnet/api/system.io.file.writealltext?view=net-5.0

您可以摆脱该行中的变量 saveData:

saveData = File.WriteAllText(savePath, savePoint);

只需使用以下方法:

File.WriteAllText(savePath, savePoint);

(请记住,您需要在使用之前实例化并分配变量 savePath 某处)


以上是无法将文本写入文件的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>