Results 1 to 7 of 7

Thread: Application Error

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2011
    Posts
    62

    Angry Application Error

    I couldn't get another reply with my other post, so i've posted my new problem. I'm trying to get a save file dialogue to overwrite a file with the same name.

    with some help I got the following code --->

    Code:
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
              If RadioButton16.Checked Then
                If IO.File.Exists("bob.txt") Then
                    Dim sfd As New SaveFileDialog
                    If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
                        IO.File.Copy("bob.txt", sfd.FileName, True)
                    End If
                End If
            End If
    I keep getting an error saying "Unhandled exception has occurred in your application." Then the next paragraph says "The process cannot access the file 'E:\Copy of Release+Fonts\bob.txt' because it is being used by another process."

    I've checked a million times, there is no other processes using bob.txt.

  2. #2
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Application Error

    Your post lacks information about the file "bob.txt", but these types of violations also occur with Read-only files. VB .NET can not write to or delete files with the property "read only" set.

    In case you want to be able to delete and write to read-only files, you will have to 'reset' the attributes of the file, and then perform the operation:
    Code:
    If System.IO.File.Exists(sfd.FileName) Then
        SetAttr(sfd.FileName, FileAttribute.Normal)
    End If
    IO.File.Copy("bob.txt", sfd.FileName, True)

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2011
    Posts
    62

    Re: Application Error

    ok, thanks for the reply. But the file is not Read-Only, i checked. and when i added the code you provided, it still gave me an error. and what more information should I provide?

  4. #4
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Application Error

    It's hard to tell what the problem is, but the OS like to set lots of rules before you can access files.

    What if you delete the file before copying?:
    Code:
    If System.IO.File.Exists(sfd.FileName) Then
        SetAttr(sfd.FileName, FileAttribute.Normal)
        IO.File.Delete(sfd.FileName)
    End If
    IO.File.Copy("bob.txt", sfd.FileName, True)
    Ow, and you could try it with using a full path instead of a relative path:

    Code:
                Dim sourcefile As String = Application.StartupPath & "\bob.txt"
                If IO.File.Exists(sourcefile) Then
                    Dim sfd As New SaveFileDialog
                    If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
                        Dim destfile As String = sfd.FileName
                        If System.IO.File.Exists(destfile) Then
                            SetAttr(destfile, FileAttribute.Normal)
                            System.IO.File.Delete(destfile)
                        End If
                        IO.File.Copy(sourcefile, destfile)
                    End If
                End If
    But, since the error is about the "bob.txt" file (saw it too late), I might have a clue of what is going on. In case you wrote the file "bob.txt" in your program, do not forget to close your writer:
    Code:
                Dim writer As New System.IO.StreamWriter("bob.txt", True)
                writer.WriteLine("line")
                writer.Close() '<- very important
    If you used the System.IO.File.WriteAllText it could be the system forgot to close the writer.

    Just in case, are you sure the file is not used by your program or was used in the past during the runtime of your program?

  5. #5
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Application Error

    Have you tried with fileinfo (also give the path to bob.txt)?
    Code:
     Dim f As New IO.FileInfo("c:\bob.txt")
            f.CopyTo("sfd.FileName", True)
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2011
    Posts
    62

    Re: Application Error

    No, I cannot provide a path because, my program is sopposed to be downloaded on different computers, and a different user might save the file to a different directory. So I cannot specify a directory, so that's why the final output is going to be in the same directory as bob.txt, which is why I have the IO.File.Exists... line. I'll try the code bergerkiller provided in the morning and see how it turns out. Thanks for the help so far! (=

  7. #7
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Application Error

    The path was hard coded by me, i assumed that you would provide it through file dialog or with the Application.StartupPath or something.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

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