我有一个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在运行时在例程中弹出,但在button5显示之前出现错误。任何关于为什么这么困难的信息都会有很大帮助。
谢谢!
答案 0 :(得分:0)
所以我们必须看到异常 - 重写代码来捕获它:
try{
file2.MoveTo(newFullPath);
DialogResult button5 = MessageBox.Show("Last Step");
}catch(Exception ex){
DialogResult button6 = MessageBox.Show(ex.ToString());
}
我认为根本不使用MessageBoxes - 使用控制台应用程序并向StdOut写消息要好得多