从调试文件中获取txt文件的问题不在调试中

问题描述 投票:0回答:2

我和一群同学正在进行一场Monogame游戏,但我们遇到了一个问题。我们正在尝试加载一个.txt文件,该文件位于我们的解决方案文件下面的文件夹中,该文件位于monogame中:

d:\ Profiles文件\ AA \来源\回购\ s2t3 \游戏\游戏

文件夹的文件路径本身是:D:\ Profiles \ aa \ Source \ Repos \ s2t3 \ Game \ Game \ AttackPattern \ ratAttackOne.txt

但是,Monogame从这个文件夹开始:

d:\ Profiles文件\ AA \来源\回购\ s2t3 \游戏\游戏\ BIN \的Windows \ 86 \调试\

我们如何能够在txt文件中写入要读取的文件名,以便进入游戏,然后进入攻击模式?

c# monogame
2个回答
0
投票

您可以使用Assembly.GetExecutingAssembly来获取调试EXE路径,并使用简单的子字符串搜索将其遍历到bin:

string debugPath = Assembly.GetExecutingAssembly().Location;
string basePath =
    debugPath.Substring(0,debugPath.IndexOf("\\bin\\", StringComparison.InvariantCultureIgnoreCase));
FileInfo fi = new FileInfo(Path.Combine(basePath, "AttackPattern", "ratAttack.txt"));

希望这可以帮助。我怀疑你知道如何加载文件。


0
投票

只需使用StreamReader读取您的txt文件,然后您可以在Project的文件夹中使用路径... \ Game \ Game \或任何您想要的,而不是在调试文件夹中。

            try
            {
                //Stream stream = TitleContainer.OpenStream("HighScore.txt");
                StreamReader streamReader = new StreamReader(
                    yourPathToTheTxtFileGoesHere));//for example, \AttackPattern\test.txt

                //do what you want to do with your txt file here

                streamReader.Close();

            }
            catch (FileNotFoundException)
            {

            }
© www.soinside.com 2019 - 2024. All rights reserved.