473,398 Members | 2,335 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,398 software developers and data experts.

Can New StreamReader create a file?

I need to get a StreamReader
Dim lsr0 As New StreamReader(FullPath)

If the file does not exist I'd like it to create one.

Do I have to Try/Catch or is there a clean way of doing it?

Thanks


Nov 21 '05 #1
4 3620
Just Me,

You do not have to look if it exist to create, however I would see if it
exist to save it in between.

Very simple made without any checking for exeception something as this.

\\\
Public Class Test
Public Shared Sub main()
Dim line As String
Dim temppath As String
Dim sr As New IO.StreamReader("C:\Herfried.xml")
If IO.File.Exists("C:\Herfried2.xml") Then
temppath = System.IO.Path.GetTempFileName
IO.File.Delete(temppath)
IO.File.Move("C:\Herfried2.xml", temppath)
End If
Dim sw As New IO.StreamWriter("C:\Herfried2.xml")
line = sr.ReadLine()
Do Until line Is Nothing
sw.WriteLine(line)
line = sr.ReadLine
Loop
If temppath <> Nothing Then
IO.File.Delete(temppath)
End If
End Sub
End Class
///
I hope this helps

Cor
Nov 21 '05 #2
Just me,

I misreaded it, however I think that I gave you every ingredient in my
previous message to do it yourself.

When not reply

Cor
Nov 21 '05 #3
" Just Me" <gr****@a-znet.com> schrieb:
I need to get a StreamReader
Dim lsr0 As New StreamReader(FullPath)

If the file does not exist I'd like it to create one.
'StreamReader' cannot create a file.
Do I have to Try/Catch or is there a clean way of doing it?


Check if the file exists using 'File.Exists' (namespace 'System.IO'). You
can create a file using 'File.Create'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
Thanks to both for the info
" Just Me" <gr****@a-znet.com> wrote in message
news:eE**************@TK2MSFTNGP14.phx.gbl...
I need to get a StreamReader
Dim lsr0 As New StreamReader(FullPath)

If the file does not exist I'd like it to create one.

Do I have to Try/Catch or is there a clean way of doing it?

Thanks

Nov 21 '05 #5

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

Similar topics

5
by: Mike | last post by:
This object wokrs great with file and file shares. But I have a virtual web site created that points to my file share where my files are located. Since this virtual web site is protected, I'd...
4
by: Rush | last post by:
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...
5
by: Anders Olsson | last post by:
I need to create a StreamReader which starts from the position of an existing StreamReader. This way, the second StreamReader can read lines "ahead" in an own loop, and then I can go back to the...
2
by: Keith Kingsley | last post by:
I'm using a StreamReader to read in several lines from an ASCII file. I'd like to know the StreamReader's "true" position-- that is, the number of bytes into the file that the StreamReader has...
16
by: vvenk | last post by:
Hello: When I use either one to read a Text file, I get the same result. The length of the string that the file's content has been written into is the same. However, if the file is binary,...
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: somequestion | last post by:
Question 1. i am using StreamReader Class like this... string str = string.Empty; StreamReader sr = new StreamReader(fsIn,Encoding.Default) while ((str = sr.ReadLine()) != null) { // wanna get...
2
by: BobRoyAce | last post by:
I am using an IO.StreamReader to read the contents of a text file. It all works just fine. However, I do not know of a way to move back to the beginning of a file. For example, let's say that I...
3
by: =?Utf-8?B?Qm9zc2ll?= | last post by:
Hi All, I am having a little trouble with a StreamReader. I am currently reading a pipe delimited file with around 1.8 million records (total size 150MB) and, based on a flag, insert update or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.