Click here to Skip to main content
14,775,260 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi guys.. im having problems on inserting text on a textfile that i have created..
here's my code:

Dim file As String = Application.StartupPath & "\" & "test.txt"
Dim mystring As String = "hello world"

If System.IO.File.Exists(file) Then
'create textfile and write default texts
Dim sw As New System.IO.StreamWriter(file)

MessageBox.Show("adto nah")
Catch ex As Exception
End Try
End If

the problems says that..cannot open file because its being used on another process..
the code works fine when text file is already created.. but when i try to create it and insert text to it.. the code do not work.. it only ends up with and error msg.. sorry for my bad english..
[no name] 24-Jul-13 11:06am
Get rid of "System.IO.File.Create(file)" and see what happens.
lalar18 24-Jul-13 11:09am
hi.. if i get rid of the,then im already assuming that the file is already there.. i was creating a program that was supposed to create first the file and then insert text to it.. but i dont know how..
[no name] 24-Jul-13 11:12am
No you are not. If your code hits that line then you are assuming that the file is NOT there as you are checking for it. The StreamWriter creates the file for you.
lalar18 24-Jul-13 11:19am
so what would be the best solution?? how am i going to create the file and then write text to it on the same time?
[no name] 24-Jul-13 11:22am
What are you talking about? Simply remove the "System.IO.File.Create(file)" line and your code works just fine! Did you even try it?
lalar18 24-Jul-13 11:29am
yes i have already try it.. and yes it works. however that's not the way my program should work.. it should be that i should first create the textfile and then insert text or numbers to it.. not that the file is already there and just insert text's or numbers to it.. tnx..
[no name] 24-Jul-13 11:40am
Well if you are not going to listen, then I am not going to waste any more of my time. Good luck.

1 solution

Simplest way is to do just this:
File.WriteAllText(Application.StartupPath & "\" & "test.txt", "Hello World")
That does pretty much everything your code does...
lalar18 24-Jul-13 11:22am
hi... i have tried your code but it says that "the specified path,file name, or both are too long. the fully qualified file name must be less than 260 characters,and the directory name must be less than 248".. but i know its near.. help please.. tnx in advance..
OriginalGriff 24-Jul-13 11:29am
Is suspect that it's your path that's the problem - not sure exactly why, but...
Please don't use the application folder to write files - it works ok in development, but tends to fail in production due to permissions problems on folders hanging off the "Program Files" directory. There is a tip here which explains where to store "real" data:
For the moment, use your temporary folder and path it absolutely:
File.WriteAllText("C:\Temp\test.txt", "Hello World")
Or whatever your Temporary folder is called (Or create a Temp folder in the root, and give it all access permissions for testing)
lalar18 24-Jul-13 11:54am
hey... i have managed to make it work!.. hehe.. tnx, but now.. another problem arises, the problem goes like how bout for example if i wanted to insert diff text or numbers on the file??

for example: i have 3 lines
1 hello
2 world
3 beautiful

tnx in advance.. :)
OriginalGriff 24-Jul-13 12:34pm
There are loads of ways to do it, depending on exactly how you want this to work.
But, basically, a string is a string, is a string, so:
File.WriteAllText("C:\Temp\test.txt", "Hello World" & vbNewLine & "Line2" & vbNewLine & "Line3")
will write 3 lines to the file, and string.Format will work as a string as well.
Or, there is File.WriteAllLines which takes an array of strings and writes each as a separate line.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900