Results 1 to 10 of 10

Thread: closing the open file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    closing the open file

    hello!
    i could i force to close the file when it is opened?

    tnx bunch

  2. #2
    Lively Member mikelynch's Avatar
    Join Date
    May 2006
    Location
    Goombungee Qld
    Posts
    83

    Re: closing the open file

    if you were using a StreamReader , it would be something like

    Code:
    sr.Close();

  3. #3
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: closing the open file

    e.g.

    StreamWriter sw = File.AppendText(Path);
    sw.WriteLine(txtInput.Text);
    sw.Close();

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: closing the open file

    here is the code:
    try
    {
    StreamReader sr = new StreamReader(txtPath.Text);
    string lineHeader = sr.ReadLine(); //disregard the heading
    string lineContents = sr.ReadLine(); //read the contents until EOF

    while (lineContents != null)
    {
    string[] strValues = lineContents.Split(',');
    listBox1.Items.Add(strValues[0] + " - " + strValues[1]);
    lineContents = sr.ReadLine();
    }
    }
    catch (Exception ex)
    {
    //MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
    }

    if the above Try found that the file is opened, how could close it on Catch?
    or close the file and then read the contents?

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: closing the open file

    EDIT: You should catch System.IO Exceptions before the general exception. And you shouldn't show ex.Message in a message box, you should do something to handle the error and show a meaningful message to the user
    Code:
            StreamReader sr = new StreamReader(txtPath.Text);
            try
            {
                string lineHeader = sr.ReadLine(); //disregard the heading
                string lineContents = sr.ReadLine(); //read the contents until EOF
    
                while ( lineContents != null )
                {
                    string[] strValues = lineContents.Split(',');
                    listBox1.Items.Add(strValues[0] + " - " + strValues[1]);
                    lineContents = sr.ReadLine();
                }
            }
            catch ( Exception ex )
            {
                //MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            finally
            {
                sr.Close();
            }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

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

    Re: closing the open file

    You should be making use of 'using' blocks so there's no need for a 'finally' block:
    C# Code:
    1. using (StreamReader sr = new StreamReader(txtPath.Text))
    2. {
    3.     try
    4.     {
    5.    
    6.     }
    7.     catch
    8.     {
    9.  
    10.     }
    11. }
    The StreamReader will be disposed and the file closed at the last closing brace no matter what happens inside the 'using' block. You should do this with all short-lived objects that require disposing.
    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

  7. #7
    Hyperactive Member
    Join Date
    Dec 2002
    Location
    The Big D
    Posts
    310

    Re: closing the open file

    Maybe the original poster is saying that when they try to open the file, it errors because it is open not because he (she) opened it and didn't close it but because some other user/process has the file open.

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: closing the open file

    Quote Originally Posted by jmcilhinney
    You should be making use of 'using' blocks so there's no need for a 'finally' block:
    C# Code:
    1. using (StreamReader sr = new StreamReader(txtPath.Text))
    2. {
    3.     try
    4.     {
    5.    
    6.     }
    7.     catch
    8.     {
    9.  
    10.     }
    11. }
    The StreamReader will be disposed and the file closed at the last closing brace no matter what happens inside the 'using' block. You should do this with all short-lived objects that require disposing.
    I'm not sure. but is the using keyword available in all versions of C#?
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

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

    Re: closing the open file

    Quote Originally Posted by ComputerJy
    I'm not sure. but is the using keyword available in all versions of C#?
    It may not be supported before 2005 but if someone's not going to bother specifying their version then I'll assume they're using the latest.
    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

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: closing the open file

    tnx for sharing ur ideas...
    to let this discussion end, when i found out the file is open
    i will close it because it keeps showing the message "already use by the other process".

    tnx to all of you
    *****************
    VB6,PHP,VS 2005

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