You are viewing our Forum Archives. To view or take place in current topics click here.
Visual Studio 2010 Writing to file using Streamwriter
Posted:

Visual Studio 2010 Writing to file using StreamwriterPosted:

ADPT
  • Rising Star
Status: Offline
Joined: Jul 25, 201112Year Member
Posts: 728
Reputation Power: 42
Status: Offline
Joined: Jul 25, 201112Year Member
Posts: 728
Reputation Power: 42
The answer to this is probably really simple and basic, however im having trouble when writing to a file as I am greeted with this error message.

"IO Exception was unhandled."

"The process cannot access the file 'C:\Users\Steven\Documents\Visual Studio 2010\Projects\SmithsSocialCare\Login Information.txt' because it is being used by another process."

This is the code in question.

Dim FileW As System.IO.StreamWriter
Dim Username As String
Dim Password As String
FileW = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\Steven\Documents\Visual Studio 2010\Projects\SmithsSocialCare\Login Information.txt", True)
If TxtPass.Text = TxtPass2.Text Then
        Username = TxtUser.Text
        Password = TxtPass.Text
        FileW.WriteLine(Username & " " & Password)
        FileW.Close()
End If


Cheers in advance, Steven.
#2. Posted:
Skittle
  • 1000 Thanks
Status: Offline
Joined: Aug 20, 20149Year Member
Posts: 6,813
Reputation Power: 413
Status: Offline
Joined: Aug 20, 20149Year Member
Posts: 6,813
Reputation Power: 413
I'm guessing you saw this example from the MSDN site, in my opinion it is not the best way to do it. Here is how I would approach using StreamWriter:


Dim path As String = "C:\Users\Steven\Documents\Visual Studio 2010\Projects\SmithsSocialCare\Login Information.txt"
Dim Username, Password As String
Dim writer As IO.StreamWriter = New StreamWriter(path)
If TxtPass.Text = TxtPass2.Text Then
    Username = TxtUser.Txt
    Password = TxtPass.Text
    writer.WriteLine(Username & " " & Password)
    writer.Flush()
    writer.Close()
End If
#3. Posted:
PMSL
  • Christmas!
Status: Offline
Joined: Jul 16, 20149Year Member
Posts: 1,504
Reputation Power: 127
Status: Offline
Joined: Jul 16, 20149Year Member
Posts: 1,504
Reputation Power: 127
I think I know whats wrong

Mke sure Above Everything put

"Imports System.IO"

That should fix it.

and your other error is caused because your program is open already go through your processes and end it when you find it then you should be good to go. if you need any more help PM me
#4. Posted:
Skittle
  • Comment King
Status: Offline
Joined: Aug 20, 20149Year Member
Posts: 6,813
Reputation Power: 413
Status: Offline
Joined: Aug 20, 20149Year Member
Posts: 6,813
Reputation Power: 413
VBC wrote I think I know whats wrong

Mke sure Above Everything put

"Imports System.IO"

That should fix it.

and your other error is caused because your program is open already go through your processes and end it when you find it then you should be good to go. if you need any more help PM me

This is not the problem, as you can see in his code he doesn't import the System.IO namespace but instead uses the whole path when declaring the FileW variable by setting the type as System.IO.StreamWriter instead of importing the namespace and simply declaring it as StreamWriter
#5. Posted:
PMSL
  • Christmas!
Status: Offline
Joined: Jul 16, 20149Year Member
Posts: 1,504
Reputation Power: 127
Status: Offline
Joined: Jul 16, 20149Year Member
Posts: 1,504
Reputation Power: 127
-Skittle wrote
VBC wrote I think I know whats wrong

Mke sure Above Everything put

"Imports System.IO"

That should fix it.

and your other error is caused because your program is open already go through your processes and end it when you find it then you should be good to go. if you need any more help PM me

This is not the problem, as you can see in his code he doesn't import the System.IO namespace but instead uses the whole path when declaring the FileW variable by setting the type as System.IO.StreamWriter instead of importing the namespace and simply declaring it as StreamWriter


Ok I didn't see that I just read the error haha my bad, I think he should change it so he used Imports it helps so much in the long run
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.