Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

StreamWriter & xml reading problem

Status
Not open for further replies.

leprogrammer

Programmer
Mar 21, 2008
25
0
0
DK
Hello.

I have this wierd problem when im writing to a text file with data from a array

I have this method:
Code:
		private void writeData()
		{
			// Open file
			StreamWriter writeData = new StreamWriter(saveMonsterSetBase.FileName, false);

			// Write all data from array to file
			for (int x = 0; x < MonsterSetBaseDataArray.Count; x++)
			{
				writeData.WriteLine(MonsterSetBaseDataArray[x]);
			}

			// Close stream
			writeData.Close();
		}

Which is trigged here:
Code:
			saveMonsterSetBase.ShowDialog();

			if (saveMonsterSetBase.FileName != "")
			{
				writeData();
			}

It works fine and opens my saveFileDialog

But after I save it I get a "file not found" on some other method I use

This is some of the method:
Code:
			// Opening txt file with location from XML
			StreamReader findMonsterStream = new StreamReader(@XMLmonsterLocation);

I get the error monster.txt (XMLmonsterLocation) is not found

It looks like the writedata changes the local path because it says

File not found in the path where I saved the file

Example:
The application and monster.txt (XMLmonsterLocation) is saved in D:\

I then use the saveFileDialog (I save a file to for example C:\) and when I access my method where I use the XMLmonsterLocation it says file not found in C:\

So I dont know why

Hope you understand

The StreamReader reads from a xml file that looks like this:
Code:
<?xml version='1.0'?>
<config>
<monsterLocation>monster.txt</monsterLocation>
<monsterSetBaseLocation>MonsterSetBase.txt</monsterSetBaseLocation>
<monsterImageLocation>mobs\</monsterImageLocation>
<mapImageLocation>maps\</mapImageLocation>
<mapInfoLocation>maps\maps.txt</mapInfoLocation>
</config>
 
are you providing the full file path to the file?
FYI wrap your object in a using block.
Code:
saveMonsterSetBase.ShowDialog();
writeData(saveMonsterSetBase.FileName);

private void writeData(string fileName)
{
   if(string.IsNullOrEmpty(fileName)
      return;

   using(StreamWriter writeData = new StreamWriter(fileName, false))
   {
      foreach(object monster in MonsterSetBaseDataArray)
      {
         writeData.WriteLine(monster );
      }
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
So from what I understand, the "current directory" is changing after you save a file. If you are using the SaveFileDialog class, try to set the following property:

Code:
saveFileDialog1.RestoreDirectory = true;

See if that works.

|| ABC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top