C# 找不到指定的文件

发布于 2024-10-11 23:57:06 字数 521 浏览 4 评论 0原文

您好,我正在尝试创建一个使用 msg.exe 通过网络发送消息的应用程序。

当我从 cmd 执行 msg 时,一切正常,但是当我使用表单打开 cmd 时,我无法执行此操作,使用 cmd 转到 system32 文件夹,该文件未显示在那里,但当我正常浏览或使用 cmd 时,它是这样的作品

在另一台计算机上测试了它,应用程序工作正常,在这台计算机上运行 win 7 64 位。

这是我用来打开 cmd 的代码示例:

Process.Start("cmd");

我以管理员身份运行,我也尝试直接从 msg.exe 执行它,它似乎是 64 位上的问题,适用于所有 32 位系统,但不适用于任何 64 位

编辑:好的,我在运行 64 位 32 位应用程序时发现问题无法在系统 32 文件夹中运行 64 位应用程序。当尝试访问此文件夹时,它会将您重定向到 %WinDir%\SysWOW64 解决方法是使用此路径 C:\Windows\Sysnative\file (%windir%\Sysnative)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

谁人与我共长歌 2024-10-18 23:57:06

问题中提到的解决方案对我来说有什么作用 - 在这里为后代发布可测试的解决方案:

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; }
}

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; }
}
智商已欠费 2024-10-18 23:57:06

你需要使用cmd吗?为什么不使用Process.Start直接调用msg.exe呢?如果您知道它在哪里,您应该能够运行它。

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.

浮世清欢 2024-10-18 23:57:06

如果您在应用程序中,最好直接执行“msg”

Process.Start("msg");

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "msg.exe";
startInfo.Arguments = "/SERVER hellowword";
startInfo.WorkingDirectory = @"C:\Temp";
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.ErrorDialog = true;
Process process = Process.Start(startInfo);

If you are in an app, you are probably better off executing "msg" directly

Process.Start("msg");

or

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "msg.exe";
startInfo.Arguments = "/SERVER hellowword";
startInfo.WorkingDirectory = @"C:\Temp";
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.ErrorDialog = true;
Process process = Process.Start(startInfo);
偏爱你一生 2024-10-18 23:57:06
Process p = new Process();
System.Diagnostics.ProcessStartInfo sinfo = new System.Diagnostics.ProcessStartInfo("C:\\Windows\\System32\\msg.exe");
p.StartInfo.Arguments=String.Format("/server:{0} {1} {2}",toServer,string.Compare(toUser.Trim(), "") == 0 ? "*" : toUser,message);
p.StartInfo = sinfo;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "msg.exe";
p.Start();

您可能需要为进程的 StartInfo 设置“用户名”和“密码”。(“msg.exe”驻留在系统文件夹中,并且运行代码的用户没有从该文件夹运行的适当权限。)

Process p = new Process();
System.Diagnostics.ProcessStartInfo sinfo = new System.Diagnostics.ProcessStartInfo("C:\\Windows\\System32\\msg.exe");
p.StartInfo.Arguments=String.Format("/server:{0} {1} {2}",toServer,string.Compare(toUser.Trim(), "") == 0 ? "*" : toUser,message);
p.StartInfo = sinfo;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "msg.exe";
p.Start();

You may need to set the "Username" and "Password" for the StartInfo of the process.("msg.exe" is residing in a system folder and the user running the code not having the appropriate permissions to run from that folder.)

欲拥i 2024-10-18 23:57:06

也许您应该检查要构建的“目标平台”。
在我的 64 位 win7 电脑上,我应该选择“x64”或“任何 CPU”,让我的代码找到“msg.exe”。

Perhaps you shoud check the "target platform" to build to.
On my 64-bit win7 pc, I should choose "x64" or "Any CPU" to let my code to find "msg.exe".

枕花眠 2024-10-18 23:57:06

在某些 Windows 版本(例如家庭版、专业版/商业版等)上,不包含 msg.exe。

On some Windows editions (e.g. Home, not Professional/Business etc.) msg.exe is not included.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文