繁体   English   中英

读写txt文件发布问题C#

[英]reading and writing txt file publish problems c#

我一直在使用外部txt文件保存比分和播放器名称(我只在两个不同的文件中保存一个比分和一个名称)

当出现新的高分时,它将节省旧的高分。 在发布该项目之前,我一直都能正常工作,现在它无法找到该文件。

我嵌入了txt文件,但无法写入。

我正在使用以下代码。

using (StreamWriter write = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "highscore.txt"))  // read the high score text file
{
    write.WriteLine(player.score); // saves the new high score
    write.Close(); // closes the file
}
using (StreamWriter write = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "nickname.txt")) // reads the high score text file
{
    write.WriteLine(player.nickname); // save new highscore name
    write.Close(); // close file
}

当我从USB或CD运行游戏时,他们不会读-

所以我的问题是-如何将txt文件放入游戏可以查看/查找的目录中?

从CD运行程序时,应用程序的路径位于该CD上,因此代码如下:

AppDomain.CurrentDomain.BaseDirectory + "highscore.txt"

指向只读CD路径。

为了能够正确保存文件,您可以:

  • 要求用户输入他/她希望将数据保存到的路径
  • 使用可用目录之一(在Environment类中查看),例如,使用:

    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

另请注意,最好使用Path.Combine()路径,而不要使用“ +”号。

暂无
暂无

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

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