0

I am using the below code to copy a file to a network location and store the file information in an access database.

Dim SqlString1 As String = "INSERT INTO document (incidentid, documentno, documentname, documentpath) VALUES (@incid, @docid, @FileName, @FilePath)"
    Using conn As New OleDbConnection(ConnString)
        conn.Open()
        If Me.TextBox1.Text <> "" Then
            'THIS WILL SAVE THE FILE TO A LOCATION    
            Dim fileLocation As String = OpenFileDialog1.FileName
            File.Copy(fileLocation, Path.Combine("\\10.1.10.5\NonConformance\Document\", Path.GetFileName(fileLocation)), True)
            'THIS WILL SAVE A FILE PATH TO THE ACCESS DB
            Using cmd As New OleDbCommand(SqlString1, conn)
                'cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@incid", doc1.Text)
                cmd.Parameters.AddWithValue("@docid", doc2.Text)
                cmd.Parameters.AddWithValue("@FileName", Path.GetFileName(Me.OpenFileDialog1.FileName))
                cmd.Parameters.AddWithValue("@FilePath", "\\10.1.10.5\NonConformance\Document\" & Path.GetFileName(Me.OpenFileDialog1.FileName))
                cmd.ExecuteNonQuery()
                conn.Close()
            End Using
        End If
        conn.Close()
    End Using

I am concerned that if a user uploads a file with the same filename as one already in the folder it will cause issues when trying to retrieve information at a later date.

So can anyone tell me how to rename the file when copying it to the network location. Ideally I would like to rename it to be the @incid_@docid parameters

Boneyt
  • 63
  • 1
  • 3
  • 11
  • You probably also need some error handling in there to handle failure to copy properly. – Trevor_G Feb 17 '17 at 15:59
  • I actually would do a file rename. The only risk is checking for an existing file and you would have the same issue with a copy as with a rename if it exists and you can check with a File.exists(). Thread on File Rename: http://stackoverflow.com/questions/19150333/how-to-rename-a-file-in-net – djangojazz Feb 17 '17 at 16:09
  • 1
    File.Copy should copy with a proper name instead of renaming after. You can look if the filename exists and append a suffix to it. Or just append the timestamp to all files, that way you don't even need to look if there's a duplicate. It bother me that you set the variable fileLocation but you don't use it the second time! – the_lotus Feb 17 '17 at 16:24
  • 1
    @the_lotus, you might want to consider editing that comment to FINAL instead of proper. – Trevor_G Feb 17 '17 at 16:29

1 Answers1

0

I ended up changing the

Path.GetFileName(fileLocation))

element to read

Path.GetFileName("INC" & Label6.Text & "DOC" & doc2.Text & ".pdf")

This will solve the issue for me, thanks for the responses.

Boneyt
  • 63
  • 1
  • 3
  • 11