473,387 Members | 1,757 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,387 software developers and data experts.

StringBuilder Append and StreamReader

I've been struggling with this for a few days now. It worked
originally as plain VB-type strings, but for some reason ceased
creating the FileNm. I changed the code to use StringBuilder, but
only get the file name, not the path.

Can someone point out my error? See notes below code.

....
Dim strFileNm As String
Dim FileNm As New StringBuilder()

If IsInString(ProgInfo.SHDataLists, "bin") Then
FileNm = FileNm.Append("c:\secondhand\datalists\")
FileNm = FileNm.Append("SHDescription.shd")
strFileNm = FileNm.ToString
Else
FileNm = FileNm.Append(ProgInfo.SHDataLists)
FileNm = FileNm.Append("SHDescription.shd")
strFileNm = FileNm.ToString
End If
MessageBox.Show(strFileNm)
Dim objReader As StreamReader = New StreamReader(strFileNm)

....

Public Function IsInString(ByVal String1 As String, ByVal String2 As
String) As Boolean
IsInString = (InStr(String1, String2) > 0)
End Function
===============
Public Class ProgInfo
Public Shared SHDataLists As String
...
End Class

SHDataLists is assigned a path of "C:\Program Files\DHSA\SecondHand\"

MessageBox only for debug purposes.
Imports System.Text has been added
=============
strFileNm = "SHDescription.shd" -- It should equal path and filename.

Crashes at StreamReader with the following
error in CLR Debugger after rebuild and install for testing:

"An unhandled exception of type 'System.IO.FileNotFoundException'
occurred in mscorlib.dll"

"Additional information: Could not find file "C:\Program
Files\DHSA\SecondHand\SHDescription.shd"."

Thanks IA.

Dan
Nov 20 '05 #1
8 5823
Dan
strFileNm = "SHDescription.shd" -- It should equal path and filename. Huh? Where? you append "SHDescription.shd" to either "C:\Program
Files\DHSA\SecondHand\" or "c:\secondhand\datalists\", so strFileNm can
never contain just "SHDescription.shd"!

Seeing as SHDataLists does not contain "bin" you are going to go through the
else, which appends "SHDescription.shd" to "C:\Program
Files\DHSA\SecondHand\"

Note rather then using a StringBuilder I would recommend using the
System.IO.Path.Combine function, as it follows set rules to combine path
parts.

Imports System.IO
Dim strFileNm As String
Dim FileNm As New StringBuilder()

If IsInString(ProgInfo.SHDataLists, "bin") Then strFileNm = Path.Combine("c:\secondhand\datalists\",
"SHDescription.shd") Else strFileNm =
Path.Combine(ProgInfo.SHDataLists,"SHDescription.s hd") End If
MessageBox.Show(strFileNm)
Hope this helps
Jay
"WordVBAProgrammer" <da**********@sba.gov> wrote in message
news:44**************************@posting.google.c om... I've been struggling with this for a few days now. It worked
originally as plain VB-type strings, but for some reason ceased
creating the FileNm. I changed the code to use StringBuilder, but
only get the file name, not the path.

Can someone point out my error? See notes below code.

...
Dim strFileNm As String
Dim FileNm As New StringBuilder()

If IsInString(ProgInfo.SHDataLists, "bin") Then
FileNm = FileNm.Append("c:\secondhand\datalists\")
FileNm = FileNm.Append("SHDescription.shd")
strFileNm = FileNm.ToString
Else
FileNm = FileNm.Append(ProgInfo.SHDataLists)
FileNm = FileNm.Append("SHDescription.shd")
strFileNm = FileNm.ToString
End If
MessageBox.Show(strFileNm)
Dim objReader As StreamReader = New StreamReader(strFileNm)

...

Public Function IsInString(ByVal String1 As String, ByVal String2 As
String) As Boolean
IsInString = (InStr(String1, String2) > 0)
End Function
===============
Public Class ProgInfo
Public Shared SHDataLists As String
...
End Class

SHDataLists is assigned a path of "C:\Program Files\DHSA\SecondHand\"

MessageBox only for debug purposes.
Imports System.Text has been added
=============
strFileNm = "SHDescription.shd" -- It should equal path and filename.

Crashes at StreamReader with the following
error in CLR Debugger after rebuild and install for testing:

"An unhandled exception of type 'System.IO.FileNotFoundException'
occurred in mscorlib.dll"

"Additional information: Could not find file "C:\Program
Files\DHSA\SecondHand\SHDescription.shd"."

Thanks IA.

Dan

Nov 20 '05 #2

Thanks, Jay. Appreciate the help.

I am getting more and more disenchanted. This is a 'learn as you go'
project at home for me, which is my favorite way of learning. It's a
part-time project.

Sorry for the delay in replying.

All attempts have worked fine until I get to the compiled exe version in
the install directory after running the setup program I built.

In the executable, as with my first attempts, it sometimes makes it to
the MessageBox with the path and file name.

However, the Streamreader only shows the filename ("SHDescription.shd")
and I get the runtime error described below.

My mind keeps telling me I'm trying to force an object and a string
together (and even ToString doesn't help) into the Streamreader. It
works fine if I hardcode the path and file name.

Ideas?

Dan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #3
Daniel,
Add the following to your code:
End If MessageBox.Show(ProgInfo.SHDataLists) MessageBox.Show(strFileNm)
Dim objReader As StreamReader = New StreamReader(strFileNm)
What is displayed for ProgInfo.SHDataLists?
However, the Streamreader only shows the filename ("SHDescription.shd")
and I get the runtime error described below. Where does it "only show" that, if you are receiving the exception you say
you are, the additional information in the exception is telling you exactly
what file you are attempting to open. Is there a file there?

Hope this helps
Jay

"Daniel Smith" <da**********@sba.gov> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Thanks, Jay. Appreciate the help.

I am getting more and more disenchanted. This is a 'learn as you go'
project at home for me, which is my favorite way of learning. It's a
part-time project.

Sorry for the delay in replying.

All attempts have worked fine until I get to the compiled exe version in
the install directory after running the setup program I built.

In the executable, as with my first attempts, it sometimes makes it to
the MessageBox with the path and file name.

However, the Streamreader only shows the filename ("SHDescription.shd")
and I get the runtime error described below.

My mind keeps telling me I'm trying to force an object and a string
together (and even ToString doesn't help) into the Streamreader. It
works fine if I hardcode the path and file name.

Ideas?

Dan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #4
Daniel,
The CLR debugger kicked in when running the executable on the
Streamreader line. On clicking Break and moving the cursor over the
variables, each shows the appropriate value (c:\program ...\datalists) "each" what variables?

What does the following show:
MessageBox.Show(ProgInfo.SHDataLists)
I had you add it, to find out what the value is. I suspect its missing the
"Datalists\" and that its only "C:\Program Files\DHSA\SecondHand\", hence
your problem.
I added Dim strFileNm As New String(FileNm.ToString()) which also
indicates "SHDescription.shd". Why?? ToString already returns a string, creating a "new string" from that
string doesn't "buy" you anything.
Also I think we need to take a major step back here and ask what you are
attempting to do with this piece of code? Not the code you have implemented,
but what the "functional requirement" you are trying to achieve. Something
like "Store all the .shd files in a folder under the folder where the main
executable is". Also what specifically ProgInfo.SHDataLists is and how does
it get set. Then we can go from there.

Hope this helps
Jay
"Daniel Smith" <da**********@sba.gov> wrote in message
news:uX**************@TK2MSFTNGP10.phx.gbl... Thanks, Jay.

Added your code, and it came up empty.

The CLR debugger kicked in when running the executable on the
Streamreader line. On clicking Break and moving the cursor over the
variables, each shows the appropriate value (c:\program ...\datalists)

BUT

The error message reads:
Additional information: Could not find file "C:\Program
Files\DHSA\SecondHand\SHDescription.shd".

Note that "Datalists\" is missing.

Streamreader(strFileNm) = "SHDescription.shd" as does the
messagebox.show(strFileNm)

I added Dim strFileNm As New String(FileNm.ToString()) which also
indicates "SHDescription.shd".

Here's what it looks like now:

If IsInString(ProgInfo.SHDataLists, "bin") Then
FileNm = FileNm.Append("c:\secondhand\datalists\")
FileNm = FileNm.Append("SHDescription.shd")
Else
FileNm = FileNm.Append(ProgInfo.SHDataLists)
FileNm = FileNm.Append("SHDescription.shd")
End If
Dim strFileNm As New String(FileNm.ToString())
MessageBox.Show(ProgInfo.SHDataLists)
MessageBox.Show(strFileNm)
Dim objReader As StreamReader = New StreamReader(strFileNm)

This is starting to affect my sanity. Seems this all started after
installing the VB .Net Resource Kit.

Thanks for your help.

Dan
VBA->VB .Net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #5
As I mentioned, but perhaps not very clearly,

Streamreader(strFileNm) = "SHDescription.shd" as does the
messagebox.show(strFileNm)

Here's the plan:

User creates (using Streamwriter), an comma delimited file of
descriptions for use in running the SecondHand time entry system of my
design.

The code locates the file in either the default install location (the
exe) or a temporary location (c:\secondhand\datalists) if I'm coding and
debugging (ergo the "bin" line).

If the file is empty, no data is loaded and the user must select a menu
option after startup to open a form to enter the data and save it to the
file. The file is then reread and the panel populated. I have several
scenarios that follow the same pattern.

The code is failing on startup because the Streamreader is not finding
the file. It runs fine in debug but not after compiling to an exe.
======

The Dim ... FileNm.ToString was added as an insane attempt to try
everything.

======
(In another module)
ProgInfo.SHDataLists = InstallDir & "Datalists\"

======
Public Class ProgInfo
Public Shared SHDataLists As String
...
End Class

======
I agree. I will have to retrace my steps. Something is rotten in
Denmark. It's just hard to find my error.

(Sometimes explaining helps find the error.)

Thanks for your help.

Dan
VBA->VB .Net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #6
Daniel,
(In another module)
ProgInfo.SHDataLists = InstallDir & "Datalists\" At runtime what is InstallDir?
I agree. I will have to retrace my steps. Something is rotten in
Denmark. It's just hard to find my error. Otherwise I'm really not sure where I can help...

Hope this helps
Jay

"Daniel Smith" <da**********@sba.gov> wrote in message
news:uQ**************@TK2MSFTNGP09.phx.gbl... As I mentioned, but perhaps not very clearly,

Streamreader(strFileNm) = "SHDescription.shd" as does the
messagebox.show(strFileNm)

Here's the plan:

User creates (using Streamwriter), an comma delimited file of
descriptions for use in running the SecondHand time entry system of my
design.

The code locates the file in either the default install location (the
exe) or a temporary location (c:\secondhand\datalists) if I'm coding and
debugging (ergo the "bin" line).

If the file is empty, no data is loaded and the user must select a menu
option after startup to open a form to enter the data and save it to the
file. The file is then reread and the panel populated. I have several
scenarios that follow the same pattern.

The code is failing on startup because the Streamreader is not finding
the file. It runs fine in debug but not after compiling to an exe.
======

The Dim ... FileNm.ToString was added as an insane attempt to try
everything.

======
(In another module)
ProgInfo.SHDataLists = InstallDir & "Datalists\"

======
Public Class ProgInfo
Public Shared SHDataLists As String
...
End Class

======
I agree. I will have to retrace my steps. Something is rotten in
Denmark. It's just hard to find my error.

(Sometimes explaining helps find the error.)

Thanks for your help.

Dan
VBA->VB .Net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #7
The objective is to take the install directory and use it to locate the
appropriate text files containing user-defined info to populate some
controls on startup.

I used a class called ProgInfo to hold the variables (i.e.
[from memory]
Private ... ProgInfo ...
Dim Shared SHDataLists as String
...
End ...)

ProgInfo.SHDatalists = InstallDir & "DataLists\"

InstallDir can be the executable directory (from Reflection) or the bin
directory during coding.

I was sailing along quite well (it does work during debug) until I
started compiling and then running the executable from the default
install directory.

Perhaps you can point me to some code which gives an example of how to
use the install directory with file open/write (Streamreader, etc).

I'm continuing to explore this, and will post a solution if I can find
one, even if I have to rewrite my code.

Again, thanks for your help. It is appreciated (especially since I'm so
close to finishing this project).

PS This is a home project (learn as you go), and I don't have it here
at work.

Dan
VBA->VB .Net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #8
Daniel,
InstallDir can be the executable directory (from Reflection) or the bin
directory during coding. I figured that is what it is defined to be, I was asking what its current
value was. ;-)

Jay

"Daniel Smith" <da**********@sba.gov> wrote in message
news:uP**************@tk2msftngp13.phx.gbl... The objective is to take the install directory and use it to locate the
appropriate text files containing user-defined info to populate some
controls on startup.

I used a class called ProgInfo to hold the variables (i.e.
[from memory]
Private ... ProgInfo ...
Dim Shared SHDataLists as String
...
End ...)

ProgInfo.SHDatalists = InstallDir & "DataLists\"

InstallDir can be the executable directory (from Reflection) or the bin
directory during coding.

I was sailing along quite well (it does work during debug) until I
started compiling and then running the executable from the default
install directory.

Perhaps you can point me to some code which gives an example of how to
use the install directory with file open/write (Streamreader, etc).

I'm continuing to explore this, and will post a solution if I can find
one, even if I have to rewrite my code.

Again, thanks for your help. It is appreciated (especially since I'm so
close to finishing this project).

PS This is a home project (learn as you go), and I don't have it here
at work.

Dan
VBA->VB .Net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #9

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

Similar topics

1
by: kevininstructor | last post by:
I found code on the web which takes data from an MS-Access table (my test table has three rows by five fields) copies it to the Clipboard then into a range within MS-Excel. Data is stored in a...
20
by: Alvin Bruney | last post by:
On the advice of a user, I've timed stringbuilder v string. Here are the results. Here are the numbers: Total # queries 3747 Time in Milliseconds StringBuilder: String...
34
by: Jerry | last post by:
Assuming we've defined StringBuilder myBuilder, which of the following is most expensive: //scenario 1 myBuilder.Append("test" + "test" + "test"); //scenario 2 myBuilder.Append("test");...
7
by: Mike P | last post by:
I read somewhere that if you are concatenating more than 2 or 3 strings you should use StringBuilder as it will use up less resources. Does this apply when you are building up a string (for...
0
by: I am Sam | last post by:
Ok I don't know what is the problem with my code But I am trying to build a newsletter that gathers parameters from 3 textbox controls and a Listbox control. The form then queries the event table...
15
by: Joe Fallon | last post by:
Is concatentaion inside of a Stringbuilder "evil"? Which is the preferred syntax below? Syntax #1 Dim sb As New StringBuilder sb.Append("Line 1" & vbCrLf) sb.Append("Line 2" & vbCrLf)...
2
by: EricD | last post by:
Are there any StringBuilder limitations that I should be aware of? I'm trying to read a 2,261 byte file into a StringBuilder object with: string fileName = info.OutputFile; FileStream...
12
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a...
8
by: Arjan | last post by:
Hello, I have been looking for a couple of days now, but I can't find anything about how to deal with StringBuilder and releasing the memory used by it. When I use stringbuilder, the memory...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.