Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# cannot find file specified

Hi I am trying to create a app that uses the msg.exe to send messages over a network.

When i execute msg from cmd everything works fine, but when i open cmd with the form i am unable to, went to the system32 folder with cmd and the file is not shown there, but when i browse or use cmd normally it is and evrything works

tested it on another computer and app works fine, running win 7 64 bit on this 1.

Here is a code sample that i use to open cmd:

Process.Start("cmd");

i am running as admin i have tried executed it directly from msg.exe aswell, it seems to be a problem on 64 bit works on all 32 bit systems but not on any 64bit

edit: ok i found the problem when running 64bit 32 bit applications cannot run 64 bit apps in the system 32 folder. when trying to access this folder it redirects you to %WinDir%\SysWOW64 a work around is to use this path C:\Windows\Sysnative\file (%windir%\Sysnative)

like image 829
Splendid Avatar asked Nov 19 '25 22:11

Splendid


2 Answers

The solution mentioned in the question was what did the trick for me - posting testable solution here for posterity:

public class Messenger : IMessenger
{
    private readonly IProcessWrapper _process;

    public Messenger(IProcessWrapper process)
    {
        _process = process;
    }

    public void SendMessage(string message)
    {
        var info = new ProcessStartInfo
            {
                WorkingDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "sysnative"),
                FileName = "msg.exe",
                Arguments = string.Format(@" * ""{0}""", message),
                WindowStyle = ProcessWindowStyle.Hidden,
                UseShellExecute = true,
                Verb = "runas"
            };
        _process.Start(info);
    }
}


public interface IProcessWrapper : IDisposable
{
    IEnumerable<Process> GetProcesses();
    void Start(ProcessStartInfo info);
    void Kill();

    bool HasExited { get; }
    int ExitCode { get; }
}
like image 66
Mathieu Guindon Avatar answered Nov 21 '25 11:11

Mathieu Guindon


Do you need to use cmd at all? Why not use Process.Start to call msg.exe directly. If you know where it is, you should be able to run it.

like image 40
Tor Haugen Avatar answered Nov 21 '25 10:11

Tor Haugen