0

I have made a program so that the user can track there fantasy football team the code below is from one of the forms that allows the user to pick their team under a specific set of rules. The issue here is not writing to the file it is reading from the file that was written too.

This file is in my resources and it contains the players the user entered. The issue is that the user has to access this text file and at this point it does not matter if the user can access edit the file or not this is a personal project for one person; however the way I have it set up this program will only run on my computer since the path I would use is the full path from the text files properties.

I have tried using the properties.Resources.resource as seen below and most recently I have tried using Application.StartupPath + "/FantasyFootball.txt". I still have not yet tried reflection because I am not sure if reflection is needed for this. The idea should be that I publish this program and it should work on any computer;however I get an exception when I try and run it outside of visual studio that the AppData folder could not be found.

So the question now is what actually happens when I press the publish button in visual studio? I get the setup file and the application files folder and the actual.application file and this ends up in my bin folder in the release folder in the app.Publish folder. I do not yet have the experience to know what this all means or what to do with it any suggestions?

private void btnShowRoster_Click(object sender, EventArgs e)
    {
        string masterRosterPath = Properties.Resources.FantasyFootball;/*
        this is where the problem is I am not sure what to make the path
        so that the user will be able to acess the file*/

 FileStream fs = new FileStream(masterRosterPath, FileMode.Open, FileAccess.Read);
        StreamReader sr = new StreamReader(fs);
        string playerIn;
        string[] fields;
        playerIn = sr.ReadLine();
        const char DELIM = ',';
        string player = " ";

        while (playerIn != null)
        {
            fields = playerIn.Split(DELIM);
            player = Convert.ToString(fields[0]);

            lbRoster.Items.Add(player);
            playerIn = sr.ReadLine();
        }
        sr.Close();
        fs.Close();
        btnShowRoster.Enabled = false;
        MessageBox.Show("Click on a player to add them to your team", "Choose Team", MessageBoxButtons.OK,MessageBoxIcon.Information);
    }
4

1 回答 1

0

根据我从您的问题中收集到的信息,您正在尝试访问您在项目的 Resources 文件夹中导入的文件。

如果我理解正确的话,访问文件资源返回的字符串FantasyFootball实际上是文本文件的内容,而不是通向它的路径。因此,您可以直接使用它的内容!无需文件阅读器。当您发布您的应用程序时,它将与它一起分发。

当您执行以下操作时,资源的内容已加载到字符串中:

string masterRosterPath = Properties.Resources.FantasyFootball;

然后就可以直接开始解析masterRosterPath了。


扩大问题

如果您决定使用资源来包含您的用户数据,您应该注意此类资源文件不允许您通过代码动态修改其内容。

例如,如果这是您的目标,您可以在磁盘上的预定路径(例如可执行文件旁边、单独的文件夹或完全其他地方)手动创建一个文件,然后再加载它(如果它不存在),或者在用户第一次运行应用程序时。然后,您可以随意加载文件、编辑其内容并保存。请注意,文件的内容在应用程序之外是可编辑的!

也许这是您要寻找的而不是资源,但是您可以使用用户设置保存该路径,而不是对其进行硬编码。这些您可以随意编辑,并且运行该应用程序的计算机的每个用户都可以具有不同的路径值。

如果您只有几个关于一个玩家的字段要保存,您甚至可以考虑将它们完全设置为设置。这将使它们易于管理和访问。您还应该注意,这些设置也可以在您的应用程序之外进行编辑,因为它们会自动保存为 XML 文件,隐藏在 AppData 文件夹中。当应用程序启动时,将为正确的用户加载设置。

如果这听起来很有趣,请查看有关使用应用程序设置和用户设置的 MSDN

希望这有帮助!

于 2013-08-28T00:02:08.847 回答