Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. StreamWriter & xml reading problem

StreamWriter & xml reading problem

Scheduled Pinned Locked Moved C#
helpdata-structuresxmltutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Casper Hansen
    wrote on last edited by
    #1

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

        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:

            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:

            // 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:

    monster.txt
    MonsterSetBase.txt
    mobs\
    maps\
    maps\maps.txt

    L 1 Reply Last reply
    0
    • C Casper Hansen

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

          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:

              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:

              // 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:

      monster.txt
      MonsterSetBase.txt
      mobs\
      maps\
      maps\maps.txt

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, Windows keeps a "current directory" for every process. There are several ways to modify the curdir, browsing with an Open/SaveFileDialog is one of them; check this: the second time you open such a dialog, it shows the folder you saw last. You can prevent this from persisting by setting FileDialog.RestoreDirectory true. In general you should not rely on the curdir, so basically your mistake is that the StreamReader uses a relative path. If you want it to always read files from your EXE's folder write code to enforce that; have a look at Application.StartUpPath :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Voting for dummies? No thanks. X|


      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups