[英]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.