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. how to write to a file when there is no exceptions.

how to write to a file when there is no exceptions.

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
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.
  • Z Offline
    Z Offline
    zxc89
    wrote on last edited by
    #1

    Hi All, i am using C#.net.And i am reading a file and then writing it to another file. The code is as follows: StreamReader reader = new StreamReader("Some Path"); StreamWriter writer = new StreamWriter( "Some Path", false ); string line; while ( (line = reader.ReadLine()) != null ) { if(some condition) { throw new ApplicationException("Unrecognised File Format"); } ..... .....some more "if" conditions.. writer.WriteLine( "\n {0} = {1},", sometext, somevalue); } writer.Close(); reader.Close(); So now if we observe,when any exception occurs, the application prints "Unrecognised File Format" and it doesn't write anything to the file.Also the output file is created first. Now what i require is: Output File should be created only when there are no exceptions in the reading of the file."Some text" and "some values" are set when using the while loop..So how can i create a file when there are no exceptions in reading. Plz Help Me!! Thanx in ADVANCE..

    L 1 Reply Last reply
    0
    • Z zxc89

      Hi All, i am using C#.net.And i am reading a file and then writing it to another file. The code is as follows: StreamReader reader = new StreamReader("Some Path"); StreamWriter writer = new StreamWriter( "Some Path", false ); string line; while ( (line = reader.ReadLine()) != null ) { if(some condition) { throw new ApplicationException("Unrecognised File Format"); } ..... .....some more "if" conditions.. writer.WriteLine( "\n {0} = {1},", sometext, somevalue); } writer.Close(); reader.Close(); So now if we observe,when any exception occurs, the application prints "Unrecognised File Format" and it doesn't write anything to the file.Also the output file is created first. Now what i require is: Output File should be created only when there are no exceptions in the reading of the file."Some text" and "some values" are set when using the while loop..So how can i create a file when there are no exceptions in reading. Plz Help Me!! Thanx in ADVANCE..

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Why don't you first entirely read the file and [b]then[/b] create the StreamWriter object. You can put the contents of the file to be read into an array first. regards

      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