473,388 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Using StreamReader when the File Doesn't exist

If i create a new StreamReader object, based on a filename that does
not exist, the SR correctly raises an exception that the file doesn't
exist. Great! I love it.

BUT....then SR proceeds to create a zero length file using the
incorrect filename parameter!!

WTF??? Is this supposed to be a feature??? Creating a file as a
result of an exception? Isn't there a flag for this?

I'll guess I'll have to pass the filename through File.Exists() first,
then bypass StreamReader if false.

Anyone else experienced this?
Nov 15 '05 #1
4 7044
Well that is the appropriate thing to do. You dont want exceptions at your
common program flow. However you can set the FileAccess and FileIO
permission on the reader so you dont get that.

Dave

"Rush" <ru***@data-tech.com> wrote in message
news:9c*************************@posting.google.co m...
If i create a new StreamReader object, based on a filename that does
not exist, the SR correctly raises an exception that the file doesn't
exist. Great! I love it.

BUT....then SR proceeds to create a zero length file using the
incorrect filename parameter!!

WTF??? Is this supposed to be a feature??? Creating a file as a
result of an exception? Isn't there a flag for this?

I'll guess I'll have to pass the filename through File.Exists() first,
then bypass StreamReader if false.

Anyone else experienced this?

Nov 15 '05 #2
Rush <ru***@data-tech.com> wrote:
If i create a new StreamReader object, based on a filename that does
not exist, the SR correctly raises an exception that the file doesn't
exist. Great! I love it.

BUT....then SR proceeds to create a zero length file using the
incorrect filename parameter!!

WTF??? Is this supposed to be a feature??? Creating a file as a
result of an exception? Isn't there a flag for this?

I'll guess I'll have to pass the filename through File.Exists() first,
then bypass StreamReader if false.

Anyone else experienced this?


Nope - and I can't reproduce it, either. Could you post some code which
demonstrates the problem? Here's my attempt to reproduce it, which
doesn't show the problem:

using System;
using System.IO;

public class Test
{
static void Main()
{
try
{
StreamReader sr = new StreamReader ("asd.txt");
}
catch (Exception e)
{
Console.WriteLine ("Exception {0}", e.Message);
}
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
I think its possible he might mean outside of a try catch block. Where if
you dont catch the exception it creates it.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Rush <ru***@data-tech.com> wrote:
If i create a new StreamReader object, based on a filename that does
not exist, the SR correctly raises an exception that the file doesn't
exist. Great! I love it.

BUT....then SR proceeds to create a zero length file using the
incorrect filename parameter!!

WTF??? Is this supposed to be a feature??? Creating a file as a
result of an exception? Isn't there a flag for this?

I'll guess I'll have to pass the filename through File.Exists() first,
then bypass StreamReader if false.

Anyone else experienced this?


Nope - and I can't reproduce it, either. Could you post some code which
demonstrates the problem? Here's my attempt to reproduce it, which
doesn't show the problem:

using System;
using System.IO;

public class Test
{
static void Main()
{
try
{
StreamReader sr = new StreamReader ("asd.txt");
}
catch (Exception e)
{
Console.WriteLine ("Exception {0}", e.Message);
}
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4
<"Dave Quigley" <merlin at countercultured dot net>> wrote:
I think its possible he might mean outside of a try catch block. Where if
you dont catch the exception it creates it.


That would require the StreamReader code to know whether or not it was
within a catch block - it's not going to know that.

Basically, I'll believe it when the OP provides an example program...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
5
by: Drew Yallop | last post by:
I read an XML file with a stream reader in VB.Net. When I look at the stream reader output in debug mode (by passing cursor over the stream reader object)the format is a perfect replica of the...
2
by: Sacha Korell | last post by:
How would I check for an end of file when parsing a text file using the StreamReader object? I would like to do something like this: '******************************** Dim objStreamReader As...
2
by: Ian Oldbury | last post by:
I'm having a problem reading from a flat file, in the file "£" and "»" exist however when i view the contents of the variable LineContents these characters don't exist. Has anyone got any...
4
by: Hexman | last post by:
Code below ---- I've asked a similar question on this forum earlier. This is a slightly different situation. Previous Question ---- I'm trying to save some specific web pages to disk as...
4
by: Gislain | last post by:
Hi, I'm trying to run .sql file with C# code, but i have systematically an error message with the "GO" instruction. When i test the script in SQL Server Management Studio, it work fine !!! ...
5
by: Rob | last post by:
Hi, I have a VB.Net application that parses an HTML file. This file was an MS Word document that was saved as web page. My application removes all unnecessary code generated by MS Word and does...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.