首页 > 解决方案 > 如何让用户输入文档然后保存该文档?

问题描述

所以我有点坚持我的项目,我正在使用 Visual Studio 并在 Visual Basic 中编码,如果有帮助的话,我也会使用 Microsoft Access 和 SQL。

我需要的是允许用户从 OpenFileDialog 中选择一个文档,然后将该文档保存到实际程序中,以便下次运行程序时它就在那里。

以下代码是在按下按钮时触发的,到目前为止我所拥有的是......

    saveDocumentDialog.Filter = "Document Files|*.docx;*.doc;*.dot;*.txt;*.rtf;*.pdf;*.ppt;*.pptx;*.xls;*.xlsx"
    saveDocumentDialog.FileName = "Untitled"
    saveDocumentDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
    saveDocumentDialog.ShowDialog()

    If saveDocumentDialog.ShowDialog = Windows.Forms.DialogResult.OK Then

        fullFilename = saveDocumentDialog.FileName

    End If

    Using openDocumentDialog As New SaveFileDialog

        Dim filename As String = IO.Path.GetFileName(fullFilename)
        openDocumentDialog.FileName = "Untitled"
        openDocumentDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
        openDocumentDialog.Title = "Select Save Location"
        openDocumentDialog.Filter = "All Files (*.*)|*.*"

        If openDocumentDialog.ShowDialog = Windows.Forms.DialogResult.OK Then

            Try

                My.Computer.FileSystem.CopyFile(fullFilename, openDocumentDialog.FileName)

            Catch ex As Exception

                MessageBox.Show("Could not copy the file." & Environment.NewLine & ex.Message, "Error copying file.", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End Try

        End If

标签: sqlvb.netdocument

解决方案


一种方法是为程序指定一个特定文件夹(例如Public Documents),并将文件保存在那里,并在程序重新启动时从该文件夹重新加载它们。您可以硬编码路径,也可以在程序中进行设置,让用户指定他们喜欢的文件夹。


推荐阅读