Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i try to execute a command line from a cmd.exe process, it doesn't work.

This is my code:
C#
public static String _getIntegrityVersion()
{
    string _version = string.Empty;

    if (Directory.Exists("C:\\Program Files (x86)\\Integrity\\") == true)
    {
        DirectoryInfo _root = new DirectoryInfo("C:\\Program Files (x86)\\Integrity\\");
        DirectoryInfo[] dir = _root.GetDirectories();

        foreach (DirectoryInfo s in dir)
        {
            if (s.Name.Contains("IntegrityClient") == true)
            {
                _version = s.Name.Substring(15);
                break;
            }
        }
    }

    return _version;
}


public static string _searchVersionOfFile(FileInfo file)
{
    string _versionIntegrity = _getIntegrityVersion();

    Process cmd = new Process();
    cmd.StartInfo.WorkingDirectory = "C:\\Program Files (x86)\\Integrity\\" + "IntegrityClient" + _versionIntegrity + "\\bin\\";
    cmd.StartInfo.FileName = "cmd.exe";
    cmd.StartInfo.RedirectStandardInput = true;
    cmd.StartInfo.RedirectStandardOutput = true;
    cmd.StartInfo.CreateNoWindow = true;
    cmd.StartInfo.UseShellExecute = false;

    cmd.Start();

    string _versionOfFile = string.Empty;
    cmd.StandardInput.WriteLine("si rlog --fields=workingrev --sandbox=" + file.Directory.Name + "\\project.pj " + file.FullName);

    cmd.StandardInput.Flush();
    cmd.StandardInput.Close();

    string s = string.Empty;

    List<string> lines = new List<string>();

    while ((s = cmd.StandardOutput.ReadLine()) != null)
    {
        lines.Add(s);
    }
    lines.Reverse();

    for (int i = 0; i < lines.Count(); i++)
        if (lines[i].Contains("=========") == true)
        {
            _versionOfFile = lines[i + 1];
            break;
        }


    cmd.WaitForExit();
    return _versionOfFile;
}


What I have tried:

Anything i've founded on internet, google, developers forums.
Nothing worked ... I need to run this cmd.exe from the specified directory ( the one from working directory) and it doesn't run from there... And i do not want to make a cmd there and run it every single time ... It's not the solution i need !
Posted
Updated 20-Nov-16 21:28pm

1 solution

The C# command to set the Curren Directory is:
C#
Directory.SetCurrentDirectory(folder);

Assuming that you analysed the problem correctly (there is some doubt), call that before you start the cmd process.
 
Share this answer
 
Comments
serbanov 21-Nov-16 4:06am    
Well, to run the command "si rlog" ( it's an IMS command in cmd ) , i need to set the Working Directory to Integrity location... and it's not setting it .. my command it's not executing and i do not know why.
serbanov 21-Nov-16 4:10am    
And i've done that before to run the cmd.start() and it has no result ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900