2014-09-02 62 views
0

我有一個C#.Net腳本,用於將文件移動到目錄並在文件名已存在時向文件名添加增量。它完全在我的包之一,但我把它抄了另一個包,它失敗,出現以下錯誤信息:FileInfo.MoveTo在SSIS包中的C#.Net腳本中生成錯誤

DTS SCript Task has encounter an exception in user code: 
    Project name: ST_<blablabla> 
    Exception has been thrown by the target of an invocation. 

    at System.RuntimeMethodHandle.InvokeMethod(Object target,Object[] arguments,Signature si, Boolean constructor) 
    at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] paramters, Object[] arguments) 
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
    at System.RuntimeType.InvokeMember(String name, BindingsFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) 
    at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine ExecuteScript() 

下面是實際的代碼:

public void Main() 
    { 
     // TODO: Add your code here 

     string fileName = Dts.Variables["LoopFiles"].Value.ToString(); 

     System.IO.FileInfo file2 = new System.IO.FileInfo(Dts.Variables["FolderPath"].Value + fileName); 

     int count = 1; 
     string fullPath =Dts.Variables["FolderPath"].Value.ToString() + Dts.Variables["LoopFiles"].Value.ToString(); 
     string fileNameOnly = Path.GetFileNameWithoutExtension(fullPath); 
     string extension = Path.GetExtension(fullPath); 
     string path = Path.GetDirectoryName(fullPath); 
     string newFullPath = fullPath; 

     while (File.Exists(newFullPath)) 
     { 
      string tempFileName = string.Format("{0}({1})", fileNameOnly, count++); 
      newFullPath = Path.Combine(path, tempFileName + extension); 
     } 

     DialogResult button3 = MessageBox.Show(file2.ToString()); 

     file2.MoveTo(newFullPath); 

     DialogResult button5 = MessageBox.Show("Last Step"); 

     Dts.TaskResult = (int)ScriptResults.Success; 
    } 

作爲一個說明,BUTTON3持久性有機污染物在運行時應該像例行程序那樣運行,但在顯示按鈕5之前會出現錯誤。任何關於爲什麼這種困難的信息都會有相當大的幫助。

謝謝!

回答

0

所以我們必須要看到異常 - 重寫代碼來捕獲它:

try{ 
    file2.MoveTo(newFullPath); 
    DialogResult button5 = MessageBox.Show("Last Step"); 
}catch(Exception ex){ 
    DialogResult button6 = MessageBox.Show(ex.ToString()); 
} 

,我認爲這是壞主意使用提示消息框的話 - 這是更好的使用控制檯應用程序,並寫郵件到stdout

+0

啊,完美!原來我在源路徑中使用了錯誤的變量。非常感謝,我計劃在MessageBoxes上使用你的建議。 – Jack 2014-09-03 12:03:59