把下面的winform 代码更改为 控制台应用程序可以使用的代码 myDevices = new DevicesCollection(); Guid driverGuid = Guid.Empty; foreach (DeviceInformation deviceInfo in myDevices) { if (!string.IsNullOrEmpty(deviceInfo.ModuleName)) { driverGuid = deviceInfo.DriverGuid; //break; } } Device dv = new Device(driverGuid); dv.SetCooperativeLevel(this, CooperativeLevel.Normal); SecondaryBuffer buf = new SecondaryBuffer("nopayhint.wav", dv); buf.Play(0, BufferPlayFlags.Default);
时间: 2024-02-16 19:00:04 浏览: 12
以下是将Winform代码更改为控制台应用程序可以使用的代码:
```
DevicesCollection myDevices = new DevicesCollection();
Guid driverGuid = Guid.Empty;
foreach (DeviceInformation deviceInfo in myDevices)
{
if (!string.IsNullOrEmpty(deviceInfo.ModuleName))
{
driverGuid = deviceInfo.DriverGuid;
break;
}
}
Device dv = new Device(driverGuid);
dv.SetCooperativeLevel(Process.GetCurrentProcess().MainWindowHandle, CooperativeLevel.Normal);
SecondaryBuffer buf = new SecondaryBuffer("nopayhint.wav", dv);
buf.Play(0, BufferPlayFlags.Default);
```
需要注意的是,由于控制台应用程序没有窗口句柄,我们需要使用`Process.GetCurrentProcess().MainWindowHandle`代替`this`来设置协作级别。
相关问题
把下面的winform 代码更改为 控制台应用程序可以使用的代码 myDevices = new DevicesCollection(); Guid driverGuid = Guid.Empty; foreach (DeviceInformation deviceInfo in myDevices) { if (!string.IsNullOrEmpty(deviceInfo.ModuleName)) { driverGuid = deviceInfo.DriverGuid; //break; } } Device dv = new Device(driverGuid); dv.SetCooperativeLevel(this, CooperativeLevel.Normal); SecondaryBuffer buf = new SecondaryBuffer("nopayhint.wav", dv); buf.Play(0, BufferPlayFlags.Default);
将上述代码转换为控制台应用程序可以使用的代码,需要进行如下修改:
1. 将 `this` 参数替换为 `null`,因为控制台应用程序没有窗体对象。
2. 去掉 `CooperativeLevel.Normal` 参数,因为在控制台应用程序中不需要设置协作级别。
3. 将 `Play` 方法的调用改为使用异步方式播放音效文件,以避免阻塞主线程。
修改后的代码如下:
```
DevicesCollection myDevices = new DevicesCollection();
Guid driverGuid = Guid.Empty;
foreach (DeviceInformation deviceInfo in myDevices)
{
if (!string.IsNullOrEmpty(deviceInfo.ModuleName))
{
driverGuid = deviceInfo.DriverGuid;
//break;
}
}
Device dv = new Device(driverGuid);
dv.SetCooperativeLevel(null, CooperativeLevel.Normal);
SecondaryBuffer buf = new SecondaryBuffer("nopayhint.wav", dv);
buf.Play(0, BufferPlayFlags.Default | BufferPlayFlags.Async);
```
在控制台应用程序中,需要将上述代码放入 `Main` 方法中执行,例如:
```
static void Main(string[] args)
{
// 音效播放代码
DevicesCollection myDevices = new DevicesCollection();
Guid driverGuid = Guid.Empty;
foreach (DeviceInformation deviceInfo in myDevices)
{
if (!string.IsNullOrEmpty(deviceInfo.ModuleName))
{
driverGuid = deviceInfo.DriverGuid;
//break;
}
}
Device dv = new Device(driverGuid);
dv.SetCooperativeLevel(null, CooperativeLevel.Normal);
SecondaryBuffer buf = new SecondaryBuffer("nopayhint.wav", dv);
buf.Play(0, BufferPlayFlags.Default | BufferPlayFlags.Async);
// 等待音效播放完成
Thread.Sleep(5000);
// 其他代码
}
```
注意,为了等待音效播放完成,上述代码使用了 `Thread.Sleep` 方法,这会阻塞主线程。在实际应用中,最好使用异步方式等待音效播放完成,以避免阻塞主线程。
winform c#调用外部EXE程序,并等待外部EXE程序执行完毕才执行下面的代码的方法。...
你可以使用C#中的Process类来调用外部EXE程序,并等待它执行完毕。以下是一个示例代码:
```csharp
Process process = new Process();
process.StartInfo.FileName = "your_exe_file.exe";
process.StartInfo.Arguments = "arguments_if_needed";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit(); // 等待外部EXE程序执行完毕
// 在这里可以写下一步要执行的代码
```
在代码中,你需要替换`your_exe_file.exe`为你要调用的外部EXE程序的文件名,如果需要传递参数,则可以在`Arguments`属性中添加。`CreateNoWindow`和`UseShellExecute`属性的设置可以确保外部EXE程序在后台运行,而不会显示在用户界面上。最后,使用`WaitForExit()`方法来等待外部EXE程序执行完毕,然后在此之后执行你希望的代码。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)