2011-08-22 154 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

相關問題