|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi. I've been working on a project and had a couple questions. I'm very new to visual basic and trying to learn as much as I can.
My first problem is writing to a file. I have a textbox on my form and a button which when clicked writes whatever is in the textbox to a certain file. The problem is...it erases everything on the file when it writes to it. How would I tell it to write to a specific line? This is my code so far: PHP Code:
My second question is how do I tell if a certain process is running? Basically I have 2 pictures on my form. One is green for online, another is red for offline. I want the form to check for a certain process "java.exe" for example. If its running I want the green picture to display for online, and if its not running then display the red picture for offline. I've been searching for awhile on how to accomplish this and can't find much help. I think you have to use something like GetProcesses but I read that was only for .net applications. Any help on either of these problems would be great. I'm sorry if its confusing, I tried to explain the best I could. |
|
#2
|
|||
|
|||
|
question 1: the StreamWriter class has an overloaded Constructor. One of those overloaded constructors will allow you to give the path to the file AND a boolean value on whether to append to the file. it is FALSE be default so it will always overwrite the contents of the file unless you tell it not to.
Code:
Using sw As StreamWriter = New StreamWriter("world.properties", true)
Last edited by eclipsed4utoo : May 2nd, 2008 at 09:07 AM. |
|
#3
|
|||
|
|||
|
Thanks for the reply Eclipse. Adding true did keep it from overwriting everything which is good. Is there a way to tell it to write to a specific line?
For example: line 2 of the file is Code:
net.sf.odinms.world.exp=600 I want to replace the "600" part of the line with the textbox1.text. I think I could do something like: Code:
Dim s As String = "net.sf.odinms.world.exp=" I'm not sure where to go after that though.. |
|
#4
|
||||
|
||||
|
No, you'd have to read the entire file, change what you want, then write it all out again, overwriting the original.
If the file is very large, you instead use two streams (instead of loading the entire file into memory) to read the original file while writing to a new one. Then at the end you delete the original and rename the new one.
__________________
Joel B Fant "An element of conflict in any discussion is a very good thing. Shows everybody's taking part, nobody left out. I like that." .NET Must-Haves Tools: Reflector References: Threading in .NET |
|
#5
|
|||
|
|||
|
I found this chunk of code that does what I want..sort of.
It replaces the line with the textbox1.text. PHP Code:
The problem is, the current line is "net.sf.odinms.world.exp=600" So then If I want to change 600 to say 900 I have to type out "net.sf.odinms.world.exp=900" in the textbox. Any suggestions? Also my file isn't big at all.....only about 1kb |
|
#6
|
||||
|
||||
|
Well, that snippet does exactly what I was describing. Instead of putting only the contents of TextBox1.Text as the second argument to Replace(), you could have this:
Code:
"next.sf.odinms.world.exp=" + TextBox1.Text The only problem is that this won't work a second time because that second line won't be "next.sf.odinms.world.exp=600", but instead "next.sf.odinms.world.exp=900". |
|
#7
|
|||
|
|||
|
Quote:
Thank you for your help LyonHaert. That'll work great for now. Maybe I can add a button that can reset that line to the default "next.sf.odinms.world.exp=" I'll just have to reset it each time I change that value. Thanks again though!! ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > .Net Development > Problem writing to a file (VB.net) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|