C#设置系统时间

在程序中有时需要系统时间准确,为了方便修改时间,做了个时间修改的,代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Qualification
...{
    public partial class FormDateSet : Form
    ...{
        public FormDateSet()
        ...{
            InitializeComponent();

        }
        //调用Kernel32.DLL
        [DllImport("Kernel32.dll")]
        public static extern void GetLocalTime(SystemTime st);
        [DllImport("Kernel32.dll")]
        public static extern void SetLocalTime(SystemTime st);


        [StructLayout(LayoutKind.Sequential)]
        public class SystemTime
        ...{
            public ushort wYear;
            public ushort

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C#设置时间录音,可以使用Windows提供的Win32 API函数来控制录音设备。以下是一个简单的示例: ```csharp using System; using System.Runtime.InteropServices; using System.Threading; class Program { // Win32 API函数声明 [DllImport("winmm.dll", SetLastError = true)] static extern uint timeSetEvent(uint uDelay, uint uResolution, TimerCallback lpTimeProc, IntPtr dwUser, uint fuEvent); [DllImport("winmm.dll", SetLastError = true)] static extern uint timeKillEvent(uint uTimerID); [DllImport("winmm.dll", SetLastError = true)] static extern uint mciSendString(string lpstrCommand, string lpstrReturnString, uint uReturnLength, IntPtr hwndCallback); // 录音参数 const int SampleRate = 44100; const int BitsPerSample = 16; const int Channels = 2; static void Main(string[] args) { // 设置录音时长为5秒 int duration = 5 * 1000; // 准备录音 string command = $"open new type waveaudio alias recsound"; mciSendString(command, null, 0, IntPtr.Zero); // 设置录音格式 command = $"set recsound format tag pcm bitspersample {BitsPerSample} channels {Channels} samplespersec {SampleRate}"; mciSendString(command, null, 0, IntPtr.Zero); // 开始录音 command = $"record recsound"; mciSendString(command, null, 0, IntPtr.Zero); // 设置录音时长 uint callbackId = timeSetEvent((uint)duration, 0, TimerCallback, IntPtr.Zero, 1); // 等待录音结束 Thread.Sleep(duration); // 停止录音 command = $"stop recsound"; mciSendString(command, null, 0, IntPtr.Zero); // 关闭录音 command = $"close recsound"; mciSendString(command, null, 0, IntPtr.Zero); // 取消时间事件 timeKillEvent(callbackId); } // 时间事件回调函数 static void TimerCallback(uint uTimerID, uint uMsg, IntPtr dwUser, IntPtr dw1, IntPtr dw2) { // 停止录音 string command = $"stop recsound"; mciSendString(command, null, 0, IntPtr.Zero); // 关闭录音 command = $"close recsound"; mciSendString(command, null, 0, IntPtr.Zero); } } ``` 在上面的示例中,我们使用timeSetEvent函数来设置录音时长,然后使用mciSendString函数来准备录音设备,设置录音格式,开始录音,停止录音和关闭录音。在时间事件回调函数中,我们停止录音并关闭录音设备。 请注意,由于使用了Win32 API函数,需要添加System.Runtime.InteropServices命名空间,并且该代码只能在Windows操作系统上运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值