2014-09-02 37 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