|
-
Jun 19th, 2007, 08:36 PM
#1
Thread Starter
Hyperactive Member
closing the open file
hello!
i could i force to close the file when it is opened?
tnx bunch
-
Jun 19th, 2007, 08:48 PM
#2
Lively Member
Re: closing the open file
if you were using a StreamReader , it would be something like
-
Jun 19th, 2007, 08:55 PM
#3
Hyperactive Member
Re: closing the open file
e.g.
StreamWriter sw = File.AppendText(Path);
sw.WriteLine(txtInput.Text);
sw.Close();
-
Jun 20th, 2007, 01:46 AM
#4
Thread Starter
Hyperactive Member
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?
-
Jun 20th, 2007, 06:03 AM
#5
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
-
Jun 20th, 2007, 07:14 AM
#6
Re: closing the open file
You should be making use of 'using' blocks so there's no need for a 'finally' block:
C# Code:
using (StreamReader sr = new StreamReader(txtPath.Text))
{
try
{
}
catch
{
}
}
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.
-
Jun 20th, 2007, 07:50 AM
#7
Hyperactive Member
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.
-
Jun 20th, 2007, 08:30 AM
#8
Re: closing the open file
 Originally Posted by jmcilhinney
You should be making use of 'using' blocks so there's no need for a 'finally' block:
C# Code:
using (StreamReader sr = new StreamReader(txtPath.Text))
{
try
{
}
catch
{
}
}
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
-
Jun 20th, 2007, 10:02 AM
#9
Re: closing the open file
 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.
-
Jun 20th, 2007, 07:08 PM
#10
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|