Results 1 to 4 of 4

Thread: VB Can't access file...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    103

    VB Can't access file...

    Hello!
    Got a problem with my project in VB 2008.

    The thing is that when i press "Save" (Button1) This message shows; "VB can't access file because it's being use from another proccess." (Or something like that)

    Here's my code:

    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim texttosave As String
            texttosave = TextBox1.Text
            Dim Filename As String
            Filename = Txtsave.Text
            Dim Location As String
            Location = Textsaveto.Text
            Dim filetype As String
            filetype = Combosave.Text
            Dim Cfile As String
            Cfile = Location & Filename & filetype
            Dim file As System.IO.FileStream
            file = System.IO.File.Create(Cfile)
    
            My.Computer.FileSystem.WriteAllText(Cfile, texttosave, True)
    
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            FolderBrowserDialog1.ShowDialog()
            Textsaveto.Text = FolderBrowserDialog1.SelectedPath & "\"
    
        End Sub
    What can I do to fix the problem?? Please help
    Thanks in advance!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB Can't access file...

    The problem is that you are trying to open the file twice. Both these lines open the file:
    Code:
            file = System.IO.File.Create(Cfile)
    
            My.Computer.FileSystem.WriteAllText(Cfile, texttosave, True)
    Mistakes like this could be easily avoided simply by reading the documentation, which seems an obvious step to me. This is from the documentation for the WriteAllText method:
    If the specified file does not exist, it is created.
    Obviously there is no need to call File.Create beforehand if WriteAllText will create the file anyway.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    103

    Re: VB Can't access file...

    Thank you! BTW I know i'm an idiot at this, but thats WHY I neeed help.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB Can't access file...

    Quote Originally Posted by TeachMeVB View Post
    Thank you! BTW I know i'm an idiot at this, but thats WHY I neeed help.
    You're not an idiot (I assume ). You're just new at it, but that's WHY you need to read the documentation. When you don't know how to do something, the first thing you should do is read the instructions. Whenever you are using a new type or member, it is a good idea to read the documentation for that type or member. When it doesn't work the way you expect, it's essential.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width