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

博客内容展示了使用System.DateTime生成文件名的代码。先是通过拼接年、月、日、时、分、秒的方式生成文件名,后又使用DateTime的ToString方法按特定格式生成文件名,属于信息技术中代码编程相关内容。
//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");
private void PasteImageToRichTextBox() { if (!Clipboard.ContainsImage()) { MessageBox.Show("剪贴板中没有图片"); return; } Dispatcher.Invoke(() => { try { // 清除提示文本 RemoveHintText(); // 图片保存路径 string ImageSavePath = "C:/ProgramData/EasyBIM/Proofread/"; // 确保目录存在 if (!Directory.Exists(ImageSavePath)) { Directory.CreateDirectory(ImageSavePath); } // 生成唯一文件名 string timestamp = DateTime.Now.ToString("yyyyMMddHHmmss"); string fileName = $"Image_{timestamp}.png"; string filePath = System.IO.Path.Combine(ImageSavePath, fileName); // 直接从剪贴板获取BitmapSource BitmapSource clipboardImage = Clipboard.GetImage(); if (clipboardImage == null) return; // 直接使用BitmapFrame创建BitmapImage BitmapImage bitmapImage; using (MemoryStream stream = new MemoryStream()) { // 使用BmpBitmapEncoder替代PngBitmapEncoder解决兼容性问题 BmpBitmapEncoder encoder = new BmpBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(clipboardImage)); encoder.Save(stream); stream.Position = 0; bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = stream; bitmapImage.EndInit(); } // 计算缩放比例 double scaleRatio = Math.Min( MaxImageWidth / bitmapImage.PixelWidth, MaxImageHeight / bitmapImage.PixelHeight ); scaleRatio = Math.Min(scaleRatio, 1.0); // 确保不放大图片 // 创建图像控件 var imageControl = new System.Windows.Controls.Image { Source = bitmapImage, Width = bitmapImage.PixelWidth * scaleRatio, Height = bitmapImage.PixelHeight * scaleRatio, Stretch = Stretch.Uniform, Margin = new Thickness(0, 5, 0, 5) }; // 创建容器 Border imageBorder = new Border { Child = imageControl, BorderBrush = Brushes.LightGray, BorderThickness = new Thickness(1), CornerRadius = new CornerRadius(4), Background = Brushes.White, Padding = new Thickness(2) }; // 创建InlineUIContainer InlineUIContainer container = new InlineUIContainer(imageBorder) { BaselineAlignment = BaselineAlignment.Center // 确保垂直居中 }; // 获取当前段落 Paragraph currentParagraph = rtbImages3.CaretPosition.Paragraph ?? new Paragraph(); // 如果是空文档则添加新段落 if (rtbImages3.Document.Blocks.Count == 0) { rtbImages3.Document.Blocks.Add(currentParagraph); } // 添加图片到段落 currentParagraph.Inlines.Add(container); // 设置新光标位置 rtbImages3.CaretPosition = container.ElementEnd; //在保存图片后,将路径关联到当前选中的行 if (pfDataGrid.SelectedItem != null) { var selectedItem = pfDataGrid.SelectedItem as ProofreadListModel; if (selectedItem != null) { selectedItem.ImagePath = filePath; } // 使用高保真方式保存原始图片 using (FileStream fileStream = new FileStream(filePath, FileMode.Create)) { PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(clipboardImage)); //使用原始剪贴板图片 encoder.Save(fileStream); } } else { rtbImages3.Document.Blocks.Clear(); MessageBox.Show("请先选择列表内相应的意见内容以关联截图"); } } catch (Exception ex) { MessageBox.Show($"图片粘贴失败: {ex.Message}"); } }); } 上述为后台代码,现在我想点击对应的pfDataGrid.SelectedItem就可以在Richtextbox中显示刚刚的截图,如何实现
09-10
为了防止目录遍历,应用程序应实施严格的访问控制,并验证用于创建路径名或创建文件访问的间接对象引用的用户输入。应用程序还应基于最小权限配置对文件系统的访问,以减少攻击的潜在影响。 只要有可能,应用程序必须限制使用用户提供的文件路径。当应用程序需要这些路径才能正常运行时,应使用间接对象引用进行文件访问,而不是接受用户的路径。例如,考虑一个允许用户从目录中指定要下载的文件的应用程序。应用程序应维护从整数键到文件名的映射,而不是使用用户提供的文件名来访问文件,确保提供的值既是整数,又对应于实际文件。如果值没有相应的文件,应用程序应返回通用错误消息。这通常会提供一种更安全的方法来引用应用程序中存在的文件,而不是试图检查用户输入是否存在恶意路径操纵。 private string WebBasePath = AppSettings.Configuration["AppSettings:ImagePath"]; /// <summary> /// 图片上传 /// </summary> /// <returns></returns> [HttpPost] [AllowAnonymous] public async Task<IActionResult> UploadImage() { try { var fileFolder = Path.Combine(WebBasePath, "picture"); if (!Directory.Exists(fileFolder)) { Directory.CreateDirectory(fileFolder); } IFormFileCollection files = Request.Form.Files; var file = files[0]; var fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(file.FileName); var filePath = Path.Combine(fileFolder, fileName); using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } return Ok(new { isValid = true, msg = "上传成功!", imageUrl = filePath, showImageUrl = _commonService.getImageBase64Url(filePath) }); } catch (Exception ex) { return Ok(new { isValid = false, msg = "上传异常!", imageUrl = "", showImageUrl = "" }); } }
07-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值