public static bool TestComand(string command, ref string outmessage)
{
string mypath = System.AppDomain.CurrentDomain.BaseDirectory;
string parameter = " -help";
string ecresult = Helper.ExeCommand(command, parameter, mypath, true, false);
if (ecresult != string.Empty)
{
outmessage = ecresult;
return false;
}
return true;
}
public static string ExeCommand(string commandName, string commandArgs, string commandWorkingDir, bool NeedOutput, bool ShowUI)
{
using (System.Diagnostics.Process process = new System.Diagnostics.Process())
{
try
{
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = commandName;
startInfo.Arguments = commandArgs.Trim();
startInfo.WorkingDirectory = commandWorkingDir;
var processEnvironment = startInfo.EnvironmentVariables;
string pathMachine = Environment.GetEnvironmentVariable("path", EnvironmentVariableTarget.Machine);
string pathUser = Environment.GetEnvironmentVariable("path", EnvironmentVariableTarget.User);
string path = (string.IsNullOrEmpty(pathMachine) ? "" : pathMachine + ";")
+ (string.IsNullOrEmpty(pathUser) ? "" : pathUser);
string oracle_homeMachine = Environment.GetEnvironmentVariable("oracle_home", EnvironmentVariableTarget.Machine);
string oracle_homeUser = Environment.GetEnvironmentVariable("oracle_home", EnvironmentVariableTarget.User);
string oracle_home = (!string.IsNullOrEmpty(oracle_homeUser)) ? oracle_homeUser : oracle_homeMachine;
if (string.IsNullOrEmpty(path))
{
if (processEnvironment.ContainsKey("path"))
{
processEnvironment.Remove("path");
}
}
else
{
processEnvironment["path"] = path;
}
if (string.IsNullOrEmpty(oracle_home))
{
if (processEnvironment.ContainsKey("oracle_home"))
{
processEnvironment.Remove("oracle_home");
}
}
else
{
processEnvironment["oracle_home"] = oracle_home;
}
if (NeedOutput)
{
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
}
if (!ShowUI)
{
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
}
process.StartInfo = startInfo;
process.StartInfo.RedirectStandardError = true;
process.Start();
process.WaitForExit();
//if exit code is 0, sucess, skip error check, else check the error message
if (process.ExitCode != 0)
{
string intererrors = process.StandardError.ReadToEnd();
string outintererror = intererrors;
while (intererrors.Length > 0)
{
if (commandName == "exp.exe")
{
int errorposition = intererrors.IndexOf("EXP-");
if (errorposition == -1)
{
break;
}
else
{
//string must exist
if (intererrors.Length > errorposition + 8)
{
string errornum = intererrors.Substring(errorposition, 9);
//this case is right, not error
if (errornum == "EXP-00091")
{
intererrors = intererrors.Substring(errorposition + 8);
continue;
}
else
{
//cut error num
string expnum = errornum.Substring(4, 5);
try
{
//parse string to int, if it is a num, it's error,else not
if (Int32.Parse(expnum) >= 0)
{
intererrors = intererrors.Substring(errorposition);
return outintererror;
}
}
catch (Exception ex)
{
//remove the message which is not error, continue
intererrors = intererrors.Substring(errorposition + 4);
}
}
}
}
}
else if (commandName == "imp.exe")
{
int errorposition = intererrors.IndexOf("IMP-");
if (errorposition == -1)
{
break;
}
else
{
//string must exist
if (intererrors.Length > errorposition + 8)
{
//cut error num
string impnum = intererrors.Substring(errorposition + 4, 5);
try
{
//parse string to int, if it is a num, it's error,else not
if (Int32.Parse(impnum) >= 0)
{
intererrors = intererrors.Substring(errorposition);
return outintererror;
}
}
catch (Exception ex)
{
//remove the message which is not error, continue
intererrors = intererrors.Substring(errorposition + 4);
}
}
}
}
}
}
if (NeedOutput)
return process.StandardOutput.ReadToEnd();
else
return string.Empty;
}
catch (Exception e)
{
using (StreamWriter fileStream = new StreamWriter("error.log"))
{
fileStream.Write(e.Message);
fileStream.Flush();
}
return e.Message;
}
}
}
public static string BuildConnectionString(string server, string name, string password)
{
return string.Format("Data Source={0};User Id={1};Password={2};Pooling=false", server, name, password);
}