将文件从当前工作目录复制到另一个文件夹

发布于 2024-11-30 12:15:49 字数 6688 浏览 1 评论 0原文

我正在尝试从 VB.NET 程序所在的当前工作目录复制文件 (pdftk.exe)。

该文件将从文件夹对话框浏览器复制到选定路径。

我尝试了这段代码,但它似乎不起作用

Imports System
Imports System.IO

Public Partial Class MainForm

    Dim Hours As Integer
    Dim Mins As Integer
    Dim Secs As Integer
    Dim Time As Integer 
    Dim nmTime As String

    Dim FileCopy As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "pdftk.exe"
    Dim FileCopyPath As String = FileCopy.ToString


    Public Sub New()

        Me.InitializeComponent()

    End Sub

    Sub Button1Click(sender As Object, e As EventArgs)



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

            txtPath.Text = dlgFolder.SelectedPath
            System.IO.File.Copy(_System.IO.Path.Combine(_System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName.CodeBase,_"pdftk.exe")_,dlgFolder.SelectedPath)

        End If

    End Sub

    Sub DlgFolderHelpRequest(sender As Object, e As EventArgs)




    End Sub

    Sub TxtPathTextChanged(sender As Object, e As EventArgs)

        txtPath.Text = dlgFolder.SelectedPath
        txtPath.Show

    End Sub

    Sub LblCurrTimeClick(sender As Object, e As EventArgs)



    End Sub

    Sub MainFormLoad(sender As Object, e As EventArgs)



        Me.timer1.Interval = 1 * 1000
        Me.timer1.Enabled = True    

        rdMerge.Checked = True



    End Sub


    Private Sub Timer1Tick(ByVal sender As System.Object,     ByVal e As System.EventArgs) 


            Me.lblCurrTime.Text = Now.ToLongTimeString

            Dim currTime As String = Now.ToLongTimeString
            Dim selTime As String = nmTime



            If currTime = selTime Then

                If Not (rdSWF.Checked = True) Then

                    Beep

                    Dim myProcess As Process

                    File.Copy("pdfmerge.py", dlgFolder.SelectedPath)


                    myProcess.Start("pdfmerge.py")

                Else 

                    Beep                

                    Dim myProcess As Process

                    File.Copy("p2f.bat", dlgFolder.SelectedPath)


                    myProcess.Start("p2f.bat")

                End If  

              End If


    End Sub

    Sub BtnSetClick(sender As Object, e As EventArgs)

        lbl8.Visible = True

        nmTime = nmHour.Value.ToString().PadLeft(2,"0") + ":" + nmMin.Value.ToString().PadLeft(2, "0") + ":" + nmSec.Value.ToString().PadLeft(2, "0")

        lblST.Text = (nmTime)

        lblST.Visible = True
        pctBox1.Visible = True

    End Sub

    Sub BtnSwfClick(sender As Object, e As EventArgs)

        Dim myProcess As Process


        Beep    

        File.Copy("p2f.bat", dlgFolder.SelectedPath)

        myProcess.Start("p2f.bat")


    End Sub

    Sub BtnNowClick(sender As Object, e As EventArgs)

        Dim myProcess As Process        
        myProcess.Start("pdfmerge.py")


    End Sub



    Sub Panel1Paint(sender As Object, e As PaintEventArgs)



    End Sub


End Class

当尝试执行该代码时,我从调试器中收到以下错误消息:

System.ArgumentException: URI formats are not supported.
   at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength)
   at System.IO.Path.GetFullPathInternal(String path)
   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName)
   at lgaPDF.MainForm.Button1Click(Object sender, EventArgs e) in C:\Documents and Settings\student3\My Documents\SharpDevelop Projects\lgaPDF\lgaPDF\MainForm.vb:line 29
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
   at System.Windows.Forms.Control.WmCommand(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
   at System.Windows.Forms.Control.DefWndProc(Message& m)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at lgaPDF.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81

程序应该从当前工作目录(程序 exe 所在的目录)获取“pdftk.exe”并将其从对话框窗口复制到选定的路径。

I'm trying to copy a file (pdftk.exe) from the Current Working Directory the VB.NET program is in.

The file is to be copied to the Selected Path from the Folder Dialog Browser.

I tried this code, but it seems it's not working

Imports System
Imports System.IO

Public Partial Class MainForm

    Dim Hours As Integer
    Dim Mins As Integer
    Dim Secs As Integer
    Dim Time As Integer 
    Dim nmTime As String

    Dim FileCopy As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "pdftk.exe"
    Dim FileCopyPath As String = FileCopy.ToString


    Public Sub New()

        Me.InitializeComponent()

    End Sub

    Sub Button1Click(sender As Object, e As EventArgs)



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

            txtPath.Text = dlgFolder.SelectedPath
            System.IO.File.Copy(_System.IO.Path.Combine(_System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName.CodeBase,_"pdftk.exe")_,dlgFolder.SelectedPath)

        End If

    End Sub

    Sub DlgFolderHelpRequest(sender As Object, e As EventArgs)




    End Sub

    Sub TxtPathTextChanged(sender As Object, e As EventArgs)

        txtPath.Text = dlgFolder.SelectedPath
        txtPath.Show

    End Sub

    Sub LblCurrTimeClick(sender As Object, e As EventArgs)



    End Sub

    Sub MainFormLoad(sender As Object, e As EventArgs)



        Me.timer1.Interval = 1 * 1000
        Me.timer1.Enabled = True    

        rdMerge.Checked = True



    End Sub


    Private Sub Timer1Tick(ByVal sender As System.Object,     ByVal e As System.EventArgs) 


            Me.lblCurrTime.Text = Now.ToLongTimeString

            Dim currTime As String = Now.ToLongTimeString
            Dim selTime As String = nmTime



            If currTime = selTime Then

                If Not (rdSWF.Checked = True) Then

                    Beep

                    Dim myProcess As Process

                    File.Copy("pdfmerge.py", dlgFolder.SelectedPath)


                    myProcess.Start("pdfmerge.py")

                Else 

                    Beep                

                    Dim myProcess As Process

                    File.Copy("p2f.bat", dlgFolder.SelectedPath)


                    myProcess.Start("p2f.bat")

                End If  

              End If


    End Sub

    Sub BtnSetClick(sender As Object, e As EventArgs)

        lbl8.Visible = True

        nmTime = nmHour.Value.ToString().PadLeft(2,"0") + ":" + nmMin.Value.ToString().PadLeft(2, "0") + ":" + nmSec.Value.ToString().PadLeft(2, "0")

        lblST.Text = (nmTime)

        lblST.Visible = True
        pctBox1.Visible = True

    End Sub

    Sub BtnSwfClick(sender As Object, e As EventArgs)

        Dim myProcess As Process


        Beep    

        File.Copy("p2f.bat", dlgFolder.SelectedPath)

        myProcess.Start("p2f.bat")


    End Sub

    Sub BtnNowClick(sender As Object, e As EventArgs)

        Dim myProcess As Process        
        myProcess.Start("pdfmerge.py")


    End Sub



    Sub Panel1Paint(sender As Object, e As PaintEventArgs)



    End Sub


End Class

When trying to execute that code I get the following error message from the debugger:

System.ArgumentException: URI formats are not supported.
   at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength)
   at System.IO.Path.GetFullPathInternal(String path)
   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName)
   at lgaPDF.MainForm.Button1Click(Object sender, EventArgs e) in C:\Documents and Settings\student3\My Documents\SharpDevelop Projects\lgaPDF\lgaPDF\MainForm.vb:line 29
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
   at System.Windows.Forms.Control.WmCommand(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
   at System.Windows.Forms.Control.DefWndProc(Message& m)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at lgaPDF.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81

The program should get "pdftk.exe" from the current working directory (the directory the program exe is in) and copy it to the selected path from the dialog window.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽心 2024-12-07 12:15:49

对于大多数用途而言,字符串是最差的数据类型。如果您无法避免使用它们,至少尝试使用能做正确事情的工具。 GetDirectoryName 返回:

大多数情况下,此方法返回的字符串由路径中直到但不包括最后一个 DirectorySeparatorChar 的所有字符组成

这意味着如果您直接将文件名连接到其末尾,您最终会得到一个路径结束LastDirectoryNamepdftk.exe,它可能不存在。

Path.Combine 是专门为将路径连接在一起并使目录分隔符正确。


您的 FileCopyPath 变量似乎特别没有意义。它需要 FileCopy,它是一个 String 并且...对其调用 ToString(),并将结果存储为 String。您期望这能实现什么目标?

类似地, FolderBrowserDialog 已经是一个字符串,那么为什么要对其调用 ToString() 呢?


我希望正确的代码会是这样的:

System.IO.File.Copy( _
    System.IO.Path.Combine( _
         System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), _
         "pdftk.exe"), _
    dlgFolder.SelectedPath)

Strings are the worst data type for most uses. Where you can't avoid using them, at least try to use tools that do the right thing. GetDirectoryName returns:

In most cases, the string returned by this method consists of all characters in the path up to but not including the last DirectorySeparatorChar

Which would mean that if you directly concatenate a filename onto the end of it, you'll end up with a path that ends LastDirectoryNamepdftk.exe, which probably doesn't exist.

Path.Combine is a method specifically designed for joining paths together and getting directory separators correct.


Your FileCopyPath variable seems particularly pointless. It takes FileCopy, which is a String and... calls ToString() on it, and stores the result as a String. What were you expecting this to achieve?

Similarly, the SelectedPath property of the FolderBrowserDialog is already a string, so why are you calling ToString() on it?


I'd expect the correct code would be something like:

System.IO.File.Copy( _
    System.IO.Path.Combine( _
         System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), _
         "pdftk.exe"), _
    dlgFolder.SelectedPath)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文