在目标计算机上呼叫另一个带有C#的EXE

发布于 2025-01-27 02:46:31 字数 1180 浏览 2 评论 0原文

我创建了一个Windows Shell扩展名,当用户右键单击File Explorer时为用户提供菜单。然后,这将调用一个小的对话应用程序。

我是软件开发的新手,所以我可能有一些男生错误,但我看不到它。

为了测试目的,我正在用力编码EXE的路径并确保它存在于VM上。

 private void CallExteralAddLocation(string sFullPath)
        {
            string sEXE = Path.Combine(Application.StartupPath, "AddLocation.exe");
            WriteLog(sEXE);
            // during testing, hard-code this path
            sEXE = "C:\\temp\\AddLocation.exe";

            System.Diagnostics.Process.Start(sEXE, sFullPath);
        }

       
        private void WriteLog(string sText)
        {
            using (StreamWriter sr = new StreamWriter("C:\\Temp\\ShellExtLog.txt"))
            {
                sr.WriteLine(sText);
                
            }
        }

它在我的主机计算机上正常工作,但是在运行Windows 10的VM运行时,这是我正在测试的地方时,对话应用程序不会被调用。

我已经检查了我可以从VM上的命令窗口中调用 addLocation.exe ,并且可以按预期工作。因此,看起来只是没有被打电话。

由于这是一个炮弹扩展,因此很难调试。我尝试使用消息框并写入控制台,但这些都无法正常工作,因此我添加了Writelog,以便对它的工作有所了解。

这在主机计算机上起作用,IE创建了日志文件,并显示了EXE的路径,但是在VM上没有创建日志文件。

注意:在两台计算机上进行测试时,我正在从应用程序的MSI重新安装。另请注意,主机正在运行Windows 11,而VM正在运行Windows 10。

因此,为什么相同的代码无法运行EXE或在VM上创建日志文件?

I have created a Windows shell extension that provides a menu to users when they right-click in File Explorer. This then invokes a small dialogue App.

I'm a novice at software development so there is probably some schoolboy error I'm making but I just can't see it.

For testing purposes I am hard-coding the path to the EXE and ensuring it's present on the VM.

 private void CallExteralAddLocation(string sFullPath)
        {
            string sEXE = Path.Combine(Application.StartupPath, "AddLocation.exe");
            WriteLog(sEXE);
            // during testing, hard-code this path
            sEXE = "C:\\temp\\AddLocation.exe";

            System.Diagnostics.Process.Start(sEXE, sFullPath);
        }

       
        private void WriteLog(string sText)
        {
            using (StreamWriter sr = new StreamWriter("C:\\Temp\\ShellExtLog.txt"))
            {
                sr.WriteLine(sText);
                
            }
        }

It works fine on my host machine but when running on a VM running Windows 10, which is where I am testing it, the dialogue App does not get invoked.

I have checked that I can invoke the AddLocation.exe from a command window on the VM and that works as expected. So it looks like it's just not getting called.

As it's a shell extension it's very difficult to debug. I tried using MessageBoxes and writing to the console but those wouldn't work, so I have added the WriteLog so that I have some idea of what it's doing.

This works on the host machine i.e. a log file is created and it shows the path to the EXE, but no log file is created on the VM.

NOTE: When testing on either machine, I'm installing afresh from the application's MSI. Also note that the host is running Windows 11 and the VM is running Windows 10.

So, why does the same code fail to either run the EXE or create a log file on the VM?

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

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

发布评论

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

评论(1

吖咩 2025-02-03 02:46:31

我最终确定了问题是C#如何处理字符串。

如果您在上面阅读了我的问题,您会看到,奇怪的是,相同的代码在Windows 11上工作正常,但在Windows 10上不正常。

问题是我让编译器解释路径字符串。我已经“逃脱”了斜线,如下所示,它应该可以正常工作:

sEXE = "C:\\temp\\AddLocation.exe";

将其更改为以下问题,以解决问题,以便现在两者都起作用:

sEXE = @"C:\temp\AddLocation.exe";

类似地,它不会在win10上创建日志文件,更改路径。到以下日志文​​件修复了以下内容:

    using (StreamWriter sr = new StreamWriter(@"C:\Temp\ShellExtLog.txt"))

I eventually worked out that the problem was how C# handles strings.

If you read through my problem above you will see that, weirdly the same code worked fine on Windows 11 but not on Windows 10.

The problem was that I was letting the compiler interpret the path string. I had 'escaped' the slashes as shown here and it should have worked fine:

sEXE = "C:\\temp\\AddLocation.exe";

Changing it to the following resolved the problem so that it works on both now:

sEXE = @"C:\temp\AddLocation.exe";

Similarly, whereas it wouldn't create a log file on Win10, changing the path to the log file to the following fixed it:

    using (StreamWriter sr = new StreamWriter(@"C:\Temp\ShellExtLog.txt"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文