Results 1 to 4 of 4

Thread: System.IO.File

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    266

    System.IO.File

    I m creating a text file with the help of File.Create() method and writing some text in it and then close it

    After this i need to open the file to review it by using the follwing sttatement

    File.OpenRead("c:\\a.txt");

    but this is not working plz can any one help me out

    the whole code that m using is as follows:

    Code:
    FileStream fs = File.Create("c:\\a.txt");
    StreamWriter sw = new StreamWriter(fs);
    
    sw.Write("hello;");
                    
    sw.Close();
    fs.Close();
    
    if (MessageBox.Show("Data has been exported successfully. Do you want to review the file?", "question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
    File.OpenRead("c:\\a.txt");

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: System.IO.File

    Always read the help documentation, especially when things don't work as you expect. 'Create' returned a FileStream, on which you had to create a StreamWriter to write the data to the file. Now that you're calling OpenRead, what do you suppose you have to do to read the data. The help topic for OpenRead even has a code example.

    I should point out the CreateText and OpenText methods are better choices for text files because they return StreamWriter and StreamReader objects directly. If you're using .NET 2.0 (please specify your version EVERY time you start a thread) then you might also look at the WriteAllText and ReadAllText methods, which don't require you to explicitly create any I/O objects at all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    266

    Re: System.IO.File

    thnx alot u r very right ... yea m using .NET 2.0 and i'm going for ur suggestions
    just one thing more if u would help ... if i need to open any file like excel or text file from my code then what should i do?
    by opening i mean to open separate note pad application sart and open a file in it . just like when we double click a text file it gets open ...

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: System.IO.File

    To open a data file in it's default application you pass the file path to Process.Start. If the file type has no default application then an exception will be thrown, so you'd have to catch that if there's a chance of that happening.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width