You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinForms中如何持久化ComboBox的音频设备选择(跨应用重启)

实现音频设备选择的持久化

1. 添加用户设置

  • 右键项目 → 属性 → 切换到「设置」选项卡
  • 若没有设置文件,点击「创建」生成
  • 添加如下设置项:
    • 名称:SelectedAudioDevice
    • 类型:string
    • 范围:用户
    • 默认值:留空即可

2. 修改窗体加载逻辑

填充完设备列表后,读取保存的设置,自动选中对应的设备。

3. 实现保存按钮逻辑

点击按钮时,将当前选中的设备名称存入设置并持久化。

修改后的完整代码

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApp2
{
    public partial class Form2 : Form
    {
        [DllImport("winmm.dll", SetLastError = true)]
        static extern uint waveInGetNumDevs();

        [DllImport("winmm.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern uint waveInGetDevCaps(uint hwo, ref WAVEOUTCAPS pwoc, uint cbwoc);

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct WAVEOUTCAPS
        {
            public ushort wMid;
            public ushort wPid;
            public uint vDriverVersion;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
            public string szPname;
            public uint dwFormats;
            public ushort wChannels;
            public ushort wReserved1;
            public uint dwSupport;
        }

        public void FillSoundDevicesInCombobox()
        {
            uint devices = waveInGetNumDevs();
            WAVEOUTCAPS caps = new WAVEOUTCAPS();
            for (uint i = 0; i < devices; i++)
            {
                waveInGetDevCaps(i, ref caps, (uint)Marshal.SizeOf(caps));
                CB1.Items.Add(caps.szPname);
            }
        }

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            FillSoundDevicesInCombobox();

            // 读取保存的设备,自动选中
            var savedDevice = Properties.Settings.Default.SelectedAudioDevice;
            if (!string.IsNullOrEmpty(savedDevice))
            {
                int targetIndex = CB1.Items.IndexOf(savedDevice);
                if (targetIndex != -1)
                {
                    CB1.SelectedIndex = targetIndex;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (CB1.SelectedItem != null)
            {
                // 保存选中的设备名称
                Properties.Settings.Default.SelectedAudioDevice = CB1.SelectedItem.ToString();
                Properties.Settings.Default.Save(); // 写入配置文件
                MessageBox.Show("设备选择已保存");
            }
            else
            {
                MessageBox.Show("请先选择一个音频设备");
            }
        }
    }
}

关键说明

  • 用WinForms内置的Properties.Settings存储设置,自动在用户目录生成配置文件,无需手动处理文件读写
  • 读取时先判断保存的设备是否存在于当前设备列表,避免因设备增减导致的选中失败
  • 必须调用Save()方法,否则设置不会持久化到本地

内容的提问来源于stack exchange,提问作者Babar Ali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 13:39:40