按日期取得文件名的两种方法 fileName=DateTime.Now.ToString("yyyyMMddhhmmss");

博客内容展示了使用System.DateTime生成文件名的代码。先是通过拼接年、月、日、时、分、秒的方式生成文件名,后又使用DateTime的ToString方法按特定格式生成文件名,属于信息技术中代码编程相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//fileName=System.DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString();
fileName=DateTime.Now.ToString("yyyyMMddhhmmss");
namespace _2 { public partial class Form2 : Form { private readonly string username; // 唯一声明 private string connectionString = "Data Source=localhost;Initial Catalog=学习管理助手;Integrated Security=True;"; private OpenFileDialog openFileDialog1 = new OpenFileDialog();//打开文件 private WebBrowser webBrowser1 = new WebBrowser(); public bool ShouldRelogin { get; private set; } = false; public Form2(string username) { InitializeComponent(); this.username = username; this.Text = $"用户管理 - {username}"; // 初始化控件 openFileDialog1.Filter = "所有文件 (*.*)|*.*"; webBrowser1.Dock = DockStyle.Fill; panel1.Controls.Add(webBrowser1); } // 修复 DeleteUserAccount 方法 private bool DeleteUserAccount() { using (SqlConnection conn = new SqlConnection(connectionString)) { try { conn.Open(); string query = "DELETE FROM T_User WHERE Uname = @Username"; using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.Add("@Username", SqlDbType.NVarChar, 50).Value = username; int rowsAffected = cmd.ExecuteNonQuery(); if (rowsAffected > 0) { MessageBox.Show($"账号 {username} 已成功注销", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); return true; } else { MessageBox.Show($"未找到用户 {username}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } } catch (SqlException sqlEx) { MessageBox.Show($"数据库错误: {sqlEx.Message}", "SQL错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } catch (Exception ex) { MessageBox.Show($"操作失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } } // 窗体关闭处理 protected override void OnFormClosing(FormClosingEventArgs e) { base.OnFormClosing(e); // 如果是退出登录/注销账号,不显示确认提示 if (ShouldRelogin) return; if (e.CloseReason == CloseReason.UserClosing) { var result = MessageBox.Show("确定要退出程序吗?", "退出确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.No) { e.Cancel = true; } } } private void 退出登录ToolStripMenuItem_Click(object sender, EventArgs e) { if (DialogResult.Yes == MessageBox.Show("确认退出吗?", "消息", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { ShouldRelogin = true; this.Close(); // 关闭当前窗体 } } private void 注销账号ToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult confirm = MessageBox.Show($"确定要永久删除账号 {username} 吗?", "确认删除", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (confirm == DialogResult.Yes && DeleteUserAccount()) { ShouldRelogin = true; this.Close(); // 关闭当前窗体 } } private void 打开ToolStripMenuItem1_Click(object sender, EventArgs e) { // 显示打开文件对话框 if (openFileDialog1.ShowDialog() == DialogResult.OK) { // 获取选中的文件路径 string filePath = openFileDialog1.FileName; try { // 在 WebBrowser 控件中显示文件 webBrowser1.Navigate(filePath); } catch (Exception ex) { MessageBox.Show("打开文件时出错:" + ex.Message); } } } private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e) { // 直接传递用户名字符串 FormUpdatePwd_User updateForm = new FormUpdatePwd_User(this.username); updateForm.ShowDialog(); } private void panel1_Paint(object sender, PaintEventArgs e) { } private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) { } private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { } private void 上传文件ToolStripMenuItem_Click(object sender, EventArgs e) { // 显示打开文件对话框 if (openFileDialog1.ShowDialog() == DialogResult.OK) { // 获取选中的文件路径 string filePath = openFileDialog1.FileName; try { // 在 WebBrowser 控件中显示文件 webBrowser1.Navigate(filePath); } catch (Exception ex) { MessageBox.Show("打开文件时出错:" + ex.Message); } } } private void 修改ToolStripMenuItem1_Click(object sender, EventArgs e) { } private void 删除ToolStripMenuItem1_Click(object sender, EventArgs e) { } } } 修改以上代码,使其具备上传笔记(文件(word或其他)并保存到界面、可归类、可保存版本历史、可分类)、可读写,可删除,可修改
07-03
class RtspToImageConverter { private string rtspUrl; private string outputDirectory; private int captureInterval; // 捕获间隔(毫秒) private CancellationTokenSource cancellationTokenSource; public RtspToImageConverter(string rtspUrl, string outputDirectory, int captureInterval = 1000) { this.rtspUrl = rtspUrl; this.outputDirectory = outputDirectory; this.captureInterval = captureInterval; this.cancellationTokenSource = new CancellationTokenSource(); // 确保输出目录存在 if (!Directory.Exists(outputDirectory)) { Directory.CreateDirectory(outputDirectory); } } public async Task StartConversionAsync() { await Task.Run(() => CaptureFrames(), cancellationTokenSource.Token); } public void StopConversion() { cancellationTokenSource.Cancel(); } private void CaptureFrames() { using (VideoCapture capture = new VideoCapture(rtspUrl)) { if (!capture.IsOpened()) { Console.WriteLine($"无法打开RTSP流: {rtspUrl}"); return; } Console.WriteLine($"开始从RTSP流捕获帧: {rtspUrl}"); Mat frame = new Mat(); int frameCount = 0; while (!cancellationTokenSource.Token.IsCancellationRequested) { try { // 读取一帧 if (!capture.Read(frame)) { Console.WriteLine("无法读取帧,可能是流已关闭"); break; } if (!frame.Empty()) { // 生成输出文件名 string fileName = $"frame_{DateTime.Now:yyyyMMdd_HHmmss_fff}.jpg"; string filePath = Path.Combine(outputDirectory, fileName); // 将帧保存为JPEG图像 frame.SaveImage(filePath); frameCount++; Console.WriteLine($"已保存帧 {frameCount}: {filePath}"); } // 等待指定的间隔时间 Thread.Sleep(captureInterval); } catch (Exception ex) { Console.WriteLine($"捕获帧时发生错误: {ex.Message}"); // 发生错误时等待一段时间再重试 Thread.Sleep(1000); } } // 释放资源 frame.Dispose(); Console.WriteLine("已停止从RTSP流捕获帧"); } } }
最新发布
07-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值