473,756 Members | 8,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Prog Info.SHDataList s, "bin") Then
FileNm = FileNm.Append(" c:\secondhand\d atalists\")
FileNm = FileNm.Append(" SHDescription.s hd")
strFileNm = FileNm.ToString
Else
FileNm = FileNm.Append(P rogInfo.SHDataL ists)
FileNm = FileNm.Append(" SHDescription.s hd")
strFileNm = FileNm.ToString
End If
MessageBox.Show (strFileNm)
Dim objReader As StreamReader = New StreamReader(st rFileNm)

....

Public Function IsInString(ByVa l 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\Seco ndHand\"

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.File NotFoundExcepti on'
occurred in mscorlib.dll"

"Additional information: Could not find file "C:\Program
Files\DHSA\Seco ndHand\SHDescri ption.shd"."

Thanks IA.

Dan
Nov 20 '05 #1
8 5836
Dan
strFileNm = "SHDescription. shd" -- It should equal path and filename. Huh? Where? you append "SHDescription. shd" to either "C:\Program
Files\DHSA\Seco ndHand\" 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\Seco ndHand\"

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(Prog Info.SHDataList s, "bin") Then strFileNm = Path.Combine("c :\secondhand\da talists\",
"SHDescription. shd") Else strFileNm =
Path.Combine(Pr ogInfo.SHDataLi sts,"SHDescript ion.shd") End If
MessageBox.Show (strFileNm)
Hope this helps
Jay
"WordVBAProgram mer" <da**********@s ba.gov> wrote in message
news:44******** *************** ***@posting.goo gle.com... 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(Prog Info.SHDataList s, "bin") Then
FileNm = FileNm.Append(" c:\secondhand\d atalists\")
FileNm = FileNm.Append(" SHDescription.s hd")
strFileNm = FileNm.ToString
Else
FileNm = FileNm.Append(P rogInfo.SHDataL ists)
FileNm = FileNm.Append(" SHDescription.s hd")
strFileNm = FileNm.ToString
End If
MessageBox.Show (strFileNm)
Dim objReader As StreamReader = New StreamReader(st rFileNm)

...

Public Function IsInString(ByVa l 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\Seco ndHand\"

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.File NotFoundExcepti on'
occurred in mscorlib.dll"

"Additional information: Could not find file "C:\Program
Files\DHSA\Seco ndHand\SHDescri ption.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.SHDat aLists) MessageBox.Show (strFileNm)
Dim objReader As StreamReader = New StreamReader(st rFileNm)
What is displayed for ProgInfo.SHData Lists?
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**********@s ba.gov> wrote in message
news:%2******** ********@tk2msf tngp13.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.SHDat aLists)
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\Seco ndHand\", hence
your problem.
I added Dim strFileNm As New String(FileNm.T oString()) 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.SHData Lists is and how does
it get set. Then we can go from there.

Hope this helps
Jay
"Daniel Smith" <da**********@s ba.gov> wrote in message
news:uX******** ******@TK2MSFTN GP10.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\Seco ndHand\SHDescri ption.shd".

Note that "Datalists\ " is missing.

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

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

Here's what it looks like now:

If IsInString(Prog Info.SHDataList s, "bin") Then
FileNm = FileNm.Append(" c:\secondhand\d atalists\")
FileNm = FileNm.Append(" SHDescription.s hd")
Else
FileNm = FileNm.Append(P rogInfo.SHDataL ists)
FileNm = FileNm.Append(" SHDescription.s hd")
End If
Dim strFileNm As New String(FileNm.T oString())
MessageBox.Show (ProgInfo.SHDat aLists)
MessageBox.Show (strFileNm)
Dim objReader As StreamReader = New StreamReader(st rFileNm)

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(st rFileNm) = "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.SHData Lists = 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.SHData Lists = 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**********@s ba.gov> wrote in message
news:uQ******** ******@TK2MSFTN GP09.phx.gbl... As I mentioned, but perhaps not very clearly,

Streamreader(st rFileNm) = "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.SHData Lists = 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.SHData lists = 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**********@s ba.gov> wrote in message
news:uP******** ******@tk2msftn gp13.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.SHData lists = 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
2275
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 StringBuilder object and should paste into Excel via Clipboard.SetDataObject. When it comes time to paste the StringBuilder object is empty. If I strip all database code out and place the code in another project it works fine. Any ideas? Thanks for...
20
1899
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 460.6624 320.4608 350.504 220.3168
34
1812
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"); myBuilder.Append("test"); myBuilder.Append("test");
7
2101
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 example an SQL query or some XML)? This is an example of some of my current code : strReturnedXML = "<?xml version=\"1.0\"?>"; strReturnedXML += "<vcn-rsp message-id=\"" + Convert.ToString(bytMessageID) + "\">";
0
1488
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 based on 2 of the textboxes one for the start date of the events required and one for the last date of the events required. The 3rd textbox works fine. It passes the text from the textbox to the stringbuilder I built on the codebehind just fine....
15
1602
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) sb.Append("Line 3" & vbCrLf)
2
1880
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 fileStream = new FileStream( fileName, FileMode.Open ); StreamReader file = new StreamReader( fileStream ); while ( file.Peek() != -1 )
12
2714
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 comparative analysis between the two and, surprisingly, string seems to out perform StringBuilder by a significant amount. A string concatenation takes not quite twice as long using StringBuilder than it does with a string. This doesn't sound right to...
8
2917
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 doesn't get released (or actually it looks like that) and thus the used memory keeps growing. Is there a solution to this? Regards, Arjan.
0
9487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9297
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9884
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7285
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.