不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
VS2008对引发异常和再次引发捕获异常的建议
编辑:dnawo 日期:2009-04-22
复制内容到剪贴板
程序代码

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace ClassLibrary1
{
public class CommandLine
{
public static string Call(string name, params string[] args)
{
string returnValue = "";
if (args.Length == 0)
throw new Exception("缺少命令行参数!");
using (Process commandline = new Process())
{
try
{
commandline.StartInfo.UseShellExecute = false;
commandline.StartInfo.CreateNoWindow = true;
commandline.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
commandline.StartInfo.RedirectStandardOutput = true;
commandline.StartInfo.FileName = name;
//commandline.StartInfo.WorkingDirectory = "";
commandline.StartInfo.Arguments = "";
//添加命令行参数
for (int i = 0; i < args.Length; i++)
{
commandline.StartInfo.Arguments += string.Format("\"{0}\" ", args[i]);//加引号防止参数带空格时出错
}
commandline.Start();
returnValue = commandline.StandardOutput.ReadToEnd().ToLower();
commandline.WaitForExit();
commandline.Close();
}
catch (Exception ex)
{
commandline.Dispose();
throw ex;
}
}
return returnValue;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace ClassLibrary1
{
public class CommandLine
{
public static string Call(string name, params string[] args)
{
string returnValue = "";
if (args.Length == 0)
throw new Exception("缺少命令行参数!");
using (Process commandline = new Process())
{
try
{
commandline.StartInfo.UseShellExecute = false;
commandline.StartInfo.CreateNoWindow = true;
commandline.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
commandline.StartInfo.RedirectStandardOutput = true;
commandline.StartInfo.FileName = name;
//commandline.StartInfo.WorkingDirectory = "";
commandline.StartInfo.Arguments = "";
//添加命令行参数
for (int i = 0; i < args.Length; i++)
{
commandline.StartInfo.Arguments += string.Format("\"{0}\" ", args[i]);//加引号防止参数带空格时出错
}
commandline.Start();
returnValue = commandline.StandardOutput.ReadToEnd().ToLower();
commandline.WaitForExit();
commandline.Close();
}
catch (Exception ex)
{
commandline.Dispose();
throw ex;
}
}
return returnValue;
}
}
}
运行代码分析后收到如下警告:

警告 4 CA2201 : Microsoft.Usage : 'CommandLine.Call(string, params string[])' 创建类型为 'Exception' 的异常,该异常类型不够具体,不应由用户代码引发。如有可能引发此异常实例,请使用其他异常类型。 E:\www\Demo\ClassLibrary1\CommandLine.cs 29 ClassLibrary1
警告 5 CA2200 : Microsoft.Usage : 'CommandLine.Call(string, params string[])' 再次引发捕获的异常并将其显式地指定为一个参数。请改用不带参数的“throw”以保留该异常最初引发时所在的堆栈位置。 E:\www\Demo\ClassLibrary1\CommandLine.cs 53 ClassLibrary1
警告 5 CA2200 : Microsoft.Usage : 'CommandLine.Call(string, params string[])' 再次引发捕获的异常并将其显式地指定为一个参数。请改用不带参数的“throw”以保留该异常最初引发时所在的堆栈位置。 E:\www\Demo\ClassLibrary1\CommandLine.cs 53 ClassLibrary1
修改如下(红色部分):
复制内容到剪贴板
程序代码

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace ClassLibrary1
{
public class CommandLine
{
public static string Call(string name, params string[] args)
{
string returnValue = "";
if (args.Length == 0)
throw new ArgumentException("缺少命令行参数!");
using (Process commandline = new Process())
{
try
{
commandline.StartInfo.UseShellExecute = false;
commandline.StartInfo.CreateNoWindow = true;
commandline.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
commandline.StartInfo.RedirectStandardOutput = true;
commandline.StartInfo.FileName = name;
//commandline.StartInfo.WorkingDirectory = "";
commandline.StartInfo.Arguments = "";
//添加命令行参数
for (int i = 0; i < args.Length; i++)
{
commandline.StartInfo.Arguments += string.Format("\"{0}\" ", args[i]);//加引号防止参数带空格时出错
}
commandline.Start();
returnValue = commandline.StandardOutput.ReadToEnd().ToLower();
commandline.WaitForExit();
commandline.Close();
}
catch
{
commandline.Dispose();
throw;
}
}
return returnValue;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace ClassLibrary1
{
public class CommandLine
{
public static string Call(string name, params string[] args)
{
string returnValue = "";
if (args.Length == 0)
throw new ArgumentException("缺少命令行参数!");
using (Process commandline = new Process())
{
try
{
commandline.StartInfo.UseShellExecute = false;
commandline.StartInfo.CreateNoWindow = true;
commandline.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
commandline.StartInfo.RedirectStandardOutput = true;
commandline.StartInfo.FileName = name;
//commandline.StartInfo.WorkingDirectory = "";
commandline.StartInfo.Arguments = "";
//添加命令行参数
for (int i = 0; i < args.Length; i++)
{
commandline.StartInfo.Arguments += string.Format("\"{0}\" ", args[i]);//加引号防止参数带空格时出错
}
commandline.Start();
returnValue = commandline.StandardOutput.ReadToEnd().ToLower();
commandline.WaitForExit();
commandline.Close();
}
catch
{
commandline.Dispose();
throw;
}
}
return returnValue;
}
}
}
建议:有时间多运行下代码分析工具,VS会给你一些很有用的建议,再针对这些建议对代码进行修改,这样能提高代码的整体性能。
评论: 0 | 引用: 0 | 查看次数: 7498
发表评论
请登录后再发表评论!