0

I am having trouble getting this to work. I have tried several different methods, but they all fail due to getting an error stating "An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll Additional information: Illegal characters in path." which I only get when I put the code in a test environment. In its intended spot it just skips without an error.

After the program gets to the using statement it just skips the rest.

        using (StreamReader sr = File.OpenText(Resource1.TextFile1))
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(sr.ReadToEnd());

            string[] resp = sb.ToString().Split('\t');

            Globals.round1 = Convert.ToDouble(resp[0]);
            Globals.round2 = Convert.ToDouble(resp[1]);
            Globals.round3 = Convert.ToDouble(resp[2]);
            Globals.round4 = Convert.ToDouble(resp[3]);
            Globals.round5 = Convert.ToDouble(resp[4]);
            Globals.round6 = Convert.ToDouble(resp[5]);
            Globals.round7 = Convert.ToDouble(resp[6]);
            Globals.round8 = Convert.ToDouble(resp[7]);
            Globals.jackpot = Convert.ToDouble(resp[8]);
        }

Thanks for any help you may be able to provide.

Jaxom Nutt
  • 49
  • 1
  • 7

2 Answers2

0

Try it that way where you can see in that link:

Firstly put your text file inside a folder. Then,

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyProject.MyProjectFolder.TextFile1.txt";

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader sr = new StreamReader(stream))
{
    StringBuilder sb = new StringBuilder();
            sb.Append(sr.ReadToEnd());

            string[] resp = sb.ToString().Split('\t');

            Globals.round1 = Convert.ToDouble(resp[0]);
            Globals.round2 = Convert.ToDouble(resp[1]);
            Globals.round3 = Convert.ToDouble(resp[2]);
            Globals.round4 = Convert.ToDouble(resp[3]);
            Globals.round5 = Convert.ToDouble(resp[4]);
            Globals.round6 = Convert.ToDouble(resp[5]);
            Globals.round7 = Convert.ToDouble(resp[6]);
            Globals.round8 = Convert.ToDouble(resp[7]);
            Globals.jackpot = Convert.ToDouble(resp[8]);

}
Community
  • 1
  • 1
Orkun Bekar
  • 1,447
  • 1
  • 15
  • 36
  • This seemed to get passed the Illegal characters error but the value from the text file seems being put in stream seems to come back as null "Additional information: Value cannot be null." – Jaxom Nutt Feb 21 '15 at 07:50
  • Did you write it like: "MyProject.Resources.TextFile1.txt"? – Orkun Bekar Feb 21 '15 at 08:01
0

I suspect Resource1.TextFile1 is actually the text file embedded as a string and not a file path.

Change

using (StreamReader sr = File.OpenText(Resource1.TextFile1))
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(sr.ReadToEnd());

...to:

StringBuilder sb = new StringBuilder();
sb.Append(Resource1.TextFile1);

The error is because you are essentially passing the contents of a text file as a file path rather than the path to the file.

The nice thing about text files added to a VIsual Studio project.Properties.Resources.Files is that you don't need to care about file paths. (actually this applies to all resources added this way)

Also, because you only do one string operation, it is not necessary to use a StringBuilder.