2011-08-22 130 views
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 

程序应该得到“ pdftk.exe“从当前工作目录(exe文件所在的目录)复制到对话窗口中的选定路径。

+0

首先第一件事情,你不应该假设在复制之前文件是否存在。我会建议你创建的路径是无效的。一旦你意识到文件不存在,你可以开始解决原因。 –

回答

0

字符串是大多数用途中最差的数据类型。如果你不能避免使用它们,至少应该尝试使用正确的工具。 GetDirectoryName回报:

在大多数情况下,此方法返回的字符串包含的所有字符的路径直到但不包括最后DirectorySeparatorChar

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

Path.Combine是一种专门用于将路径连接在一起并获取目录分隔符正确的方法。


您的FileCopyPath变量显得特别没有意义。它需要FileCopy,这是一个String和...上调用ToString(),并将结果存储为String。你期待这个成就是什么?

同样,FolderBrowserDialogSelectedPath属性已经是一个字符串,那么为什么你要调用ToString()呢?


我预计正确的代码会是这样的:

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

我编辑了代码(在主要问题中编辑),并从调试器中获取错误消息(也编辑了iin主要问题) – Brian

+0

@Brian - 我将下划线放入,因为我将代码分成多行,而VB .Net在2010版之前并没有让你跨多行破解代码。如果你把它全部放在一行上,删除'_'字符。 –

+0

当我尝试运行程序时,它给了我一个未处理的异常,告诉我“不支持URI格式”。代码工作正常,给我没有调试错误,但文件也不复制。 – Brian

相关问题