StringBuilder Append and StreamReader

  • Thread starter Thread starter WordVBAProgrammer
  • Start date Start date
W

WordVBAProgrammer

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
 
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")
strFileNm =
Path.Combine(ProgInfo.SHDataLists,"SHDescription.shd")
End If
MessageBox.Show(strFileNm)

Hope this helps
Jay
 
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
 
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,
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
 
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
 
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
 
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
 
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 said:
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
 
Back
Top