简体   繁体   中英

create file to another location in vb.net

I create a plugin for MS Word by VSTO VB.NET I Wrote the function to copy two files to the AppData folder from Resources. The code works fine and copy files, but create the Additional files (file size is 0) in MyDocumnet and my doc file location. How can I fix it?

Public Function openFile(fName As String) As String
    Dim path, fileName As String
    Dim bytes, p
    ' Dim FileLocked As Boolean
    p = Environment.GetEnvironmentVariable("APPDATA") & "\"

    Select Case fName
        Case "q"
            bytes = My.Resources.qText
            fileName = "qText.docx"
            path = p & fileName  
        Case "t"
            bytes = My.Resources.tText
            fileName = "tText.docx"
            path = p & fileName
           
    End Select

    Dim Locked As Boolean = False
    Try
        Dim fs As FileStream = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)
        fs.Close()
    Catch
        Locked = True
    End Try
    Try
        If Locked Then
            Return fileName
        Else
            File.WriteAllBytes(path, bytes)
            If fileName = "QText.docx" Then
                SourceApp.Documents.Open(FileName:=path, ReadOnly:=True, Visible:=False)
            Else
                SourceApp.Documents.Open(FileName:=path, Visible:=False)
                SourceApp.Documents("tText.docx").Content.Delete()
            End If
            SourceApp.ScreenUpdating = False
            SourceApp.DisplayStatusBar = False
            Call ComMode()
            Return fileName
        End If
    Catch ex As Exception
    End Try
End Function

Shouldn't this:

Dim fs As FileStream = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)

actually be this:

Dim fs As FileStream = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)

As it stands, you're specifying only a file name rather than a file path, so the folder path has to be assumed to be some default, which is presumably where you're seeing those files created.

This is an example of why descriptive variable names are important. Personally, I would have used folderPath , fileName and filePath rather than p , fileName and path . It's far more obvious what each one is then.

What's the point of creating a file anyway? Why not check whether one exists first and then only try to open it if it does? You appear to be checking whether the file is locked but it obviously can't be locked if it doesn't exist.

When you check whether a particular file exists/locked on the disk a relative path is used. Only the filename is passed which means the relative path:

Dim fs As FileStream = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)
        fs.Close()

But when you write the content the absolute path is specified in the code:

File.WriteAllBytes(path, bytes)

The path can point to another place. I'd suggest using the Directory.GetCurrentDirectory method which gets the current working directory of the application. If required you may set the current directory using the Environment.CurrentDirectory property sets the fully qualified path of the current working directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM