.Net Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - More.Net Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 1st, 2008, 06:54 PM
ajb ajb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 4 ajb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 42 sec
Reputation Power: 0
Red face Problem writing to a file (VB.net)

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:
Private Sub Button3_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles Button3.Click
        
If File.Exists("world.properties"Then
            Using sw 
As StreamWriter = New StreamWriter("world.properties")
                
sw.Write(TextBox1.Text)
            
End Using
        End 
If
End Sub 


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.

Reply With Quote
  #2  
Old May 2nd, 2008, 09:05 AM
eclipsed4utoo eclipsed4utoo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 143 eclipsed4utoo User rank is Sergeant Major (2000 - 5000 Reputation Level)eclipsed4utoo User rank is Sergeant Major (2000 - 5000 Reputation Level)eclipsed4utoo User rank is Sergeant Major (2000 - 5000 Reputation Level)eclipsed4utoo User rank is Sergeant Major (2000 - 5000 Reputation Level)eclipsed4utoo User rank is Sergeant Major (2000 - 5000 Reputation Level)eclipsed4utoo User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 21 h 51 m 14 sec
Reputation Power: 26
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.

Reply With Quote
  #3  
Old May 2nd, 2008, 01:50 PM
ajb ajb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 4 ajb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 42 sec
Reputation Power: 0
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..

Reply With Quote
  #4  
Old May 2nd, 2008, 02:06 PM
LyonHaert's Avatar
LyonHaert LyonHaert is offline
Arcane Scribbler
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2005
Location: Indianapolis, IN
Posts: 1,783 LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 14 h 17 m 53 sec
Reputation Power: 446
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

Reply With Quote
  #5  
Old May 2nd, 2008, 02:55 PM
ajb ajb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 4 ajb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 42 sec
Reputation Power: 0
I found this chunk of code that does what I want..sort of.
It replaces the line with the textbox1.text.

PHP Code:
Private Sub Button3_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles Button3.Click
        Dim FileContents 
As String
        Dim objReader 
As System.IO.StreamReader
        objReader 
= New StreamReader(Environment.CurrentDirectory "\world.properties")
        
FileContents objReader.ReadToEnd
        objReader
.Close()
        
FileContents FileContents.Replace("net.sf.odinms.world.exp=600", (TextBox1.Text))
        
Dim objWriter As System.IO.StreamWriter
        objWriter 
= New StreamWriter(Environment.CurrentDirectory "\world.properties")
        
objWriter.Write(FileContents)
        
objWriter.Close()
End Sub 


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

Reply With Quote
  #6  
Old May 2nd, 2008, 03:20 PM
LyonHaert's Avatar
LyonHaert LyonHaert is offline
Arcane Scribbler
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2005
Location: Indianapolis, IN
Posts: 1,783 LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)LyonHaert User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 14 h 17 m 53 sec
Reputation Power: 446
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
Then the only thing you need in TextBox1.Text is "900".

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".

Reply With Quote
  #7  
Old May 2nd, 2008, 04:54 PM
ajb ajb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 4 ajb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 42 sec
Reputation Power: 0
Quote:
Originally Posted by LyonHaert
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
Then the only thing you need in TextBox1.Text is "900".

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".


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!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - More.Net Development > Problem writing to a file (VB.net)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT