【问题标题】:An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in mscorlib.dllmscorlib.dll 中出现“System.ComponentModel.Win32Exception”类型的未处理异常
【发布时间】:2017-11-29 05:51:29
【问题描述】:

我在 WPF 窗口中托管了一个带有 WF 控件的应用程序,并且我正在我的项目中进行一些记录和播放操作。播放时出现以下异常。

系统找不到指定的文件

我已经对此进行了搜索,并看到了一些建议,我们需要提供要处理的文件的完整路径。我也尝试过,但仍然遇到此异常。

请任何人告诉我如何解决这个问题?

var instance = testclass.GetControlForTest();
WindowsFormsHost host = new WindowsFormsHost();
host.Child = instance;

var testwindow = new TestWindow(testdata, testclass, false, (MainWindow)Application.Current.MainWindow, true);
testwindow.testSurface.Children.Add(host);

testwindow.Show();
await Task.Delay(1000);
Process p = new Process();
var mainPath = Directory.GetCurrentDirectory();

do
{
    mainPath = Directory.GetParent(mainPath).FullName;
} while (Directory.GetParent(mainPath).Name != "WindowsForms_Testing");
var targetPath = mainPath + "\\exes\\" + "\\";
mainPath = Directory.GetParent(mainPath).FullName;
mainPath = mainPath + "\\TestStudio\\Syncfusion.TestVisualBase.WPF\\RecordAction\\RecordAction.WPF\\bin\\Debug\\RecordAction.WPF.exe";
p.StartInfo.FileName = mainPath.Replace("\\", "/");
p.StartInfo.Arguments = file; //("..//..//test.xml")
p.Start();

谢谢,

【问题讨论】:

  • 附注:为什么不使用 Path.Combine ?路径前后不需要所有这些斜线......让您的生活更轻松。

标签: c# wpf winforms


【解决方案1】:
  1. 不应替换反斜杠 p.StartInfo.FileName = mainPath.Replace("\\", "/");,因为 Windows 使用 \ 表示路径
  2. 您定义变量var targetPath ut 以后不要使用它。
  3. 如果您想将路径放入变量中,您可以使用@ 转义特殊符号,例如mainPath = mainPath + @"\TestStudio\Syncfusion.TestVisualBase.WPF\RecordAction\RecordAction.WPF\bin\Debug\RecordAction.WPF.exe";
  4. 对于连接路径,您可以使用Path.Combine() 方法
  5. 属性p.StartInfo.FileName 的路径必须从光盘名称开始,例如C:\,或点/秒,例如.\your_path..\parent_directory\your_path 或直接从文件或文件夹开始,相对当前目录Directory.GetCurrentDirectory();

【讨论】:

  • 嗨@Anton,感谢您的建议。现在我不会像你说的那样替换反斜杠。下面是我使用的路径,“E:\TestStudio\WindowsForms_Testing\TestStudio\Syncfusion.TestVisualBase.WPF\RecordAction\RecordAction.WPF\bin\Debug\RecordAction.WPF.exe”但我仍然得到同样的错误。有什么事吗?
  • 如果我这样给出,p.StartInfo.FileName = @"E:\TestStudio\WindowsForms_Testing\WPF\TestStudio\Syncfusion.TestVisualBase.WPF\RecordAction\RecordAction.WPF\bin\Debug\ RecordAction.WPF.exe";它工作正常。我的实际实现有什么问题吗?
  • 第一个和第二个 cmets 中的路径不同。在第一种情况下,您使用文件夹 "E:\TestStudio\WindowsForms_Testing\TestStudio" ,但在第二次 - E:\TestStudio\WindowsForms_Testing\ WPF \TestStudio\
  • 嗨@Anton,感谢您宝贵的时间。我意识到我的错并改正了。现在它工作正常。
猜你喜欢
  • 2014-03-09
  • 2015-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多