Accessing a text file from resource folder in MS visual studio.

Started by
4 comments, last by BCullis 11 years, 2 months ago

Ive got this text file Im trying to read from. Its located in my resources folder in microsoft visual studio. Im trying to access that text file so I can parse the text from that file to an integer and store it in my integer array called Armor[,] .The contents of the text file is a 12x11 chart of numbers which I would like to parse to an integer and store into the 2D Armor[,] array. Here are the contents of the text file:

http://pastebin.com/Nq4XFCYM

When I run the program I get an error saying:

Value cannot be null.
Parameter name: path

The error that microsoft visual studio's throwing is because of the line:

[source]StreamReader armorStream = new StreamReader(Resources.Armor);[/source]

[source]

//namespace used do directly access Resource.FileName

using TurfBattles.Properties;

public void LoadArmor()
{
//Create a variable for storing the armor defense values
int[,] Armor = new int[12, 11];

//Create a string variable for storing the stream readers input

string myString = new string();


int Row = 0;

//Create a stream reader for reading the Armor.txt file
StreamReader armorStream = new StreamReader(Resources.Armor);

//While the stream reader is able to read the next line
//do this:
while ((myString = armorStream.ReadLine()) != null)
{
//Loop through each value of the myStringSplit array
for (int Column = 0; Column < 11; Column++)
{
//This splits the text up and stores it into an array of strings
//It also stores the white spaces into a character array
string[] myStringSplit = myString.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);

//Store the current value of myStringSplit into the Armor Array
Armor[Row, Column] = int.Parse(myStringSplit[Column]);
}
Console.WriteLine();

Row++;
}
armorStream.Close();

Console.ReadKey();
}
[/source]

Advertisement

Could we see the text file content?

http://pastebin.com/Nq4XFCYM

http://pastebin.com/Nq4XFCYM

It seems ReadLine is expecting a '\r' or '\n', do you have it in your file? If the \r or \n isn't found and it reaches the eof, a null is returned, this fits with your issue...

From http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx

A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r"), or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does not contain the terminating carriage return or line feed. The returned value is null if the end of the input stream is reached.

When I run the program I get an error saying:

Value cannot be null.
Parameter name: path

The error that microsoft visual studio's throwing is because of the line:

[source]StreamReader armorStream = new StreamReader(Resources.Armor);[/source]

The problem is that I am trying to access a file, but the arguments within the parameters of the stream reader takes a file path.

When that error is thrown, what's the value of "Resources.Armor"? VS IDE should let you hover over to check.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

This topic is closed to new replies.

Advertisement