我尝试使用特殊名称保存我的文件" MDlabs" +我的vb.net项目中的数据和时间 我尝试做的是像这样保存文件MDlabs_yyyy / MM / dd HH:mm:ss.jpg 但它给了我一个错误 这是错误
{"不支持指定的路径格式。"}
这是我的代码
Dim mydataandtimeforsave = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
PB1.Save(My.Computer.FileSystem.SpecialDirectories.Desktop + "\MDAL1Image_" + mydataandtimeforsave + ".jpg", System.Drawing.Imaging.ImageFormat.Bmp)
答案 0 :(得分:0)
您无法在文件名中使用/ :(某些特殊字符)。这是一般限制。尝试其他日期格式
答案 1 :(得分:0)
您无法在文件名中使用某些字符。在您使用代码中的字符的情况下,您可以使用其他字符(例如-
)来分隔您感兴趣的部分。您可以按如下方式清理文件名
Dim tempName = Date.Now.ToString("yyyy/MM/dd HH:mm:ss") ' Using your example but this could also come from user input.
Dim pattern = $"[{String.Join("", Path.GetInvalidFileNameChars())}]" ' Assumes Imports System.IO at the top
Dim fileName = Regex.Replace(tempName, pattern, "-") ' Assumes Imports System.Text.RegularExpressions at the top
然后您可以按如下方式保存整个文件......
PB1.Save(Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "MDAL1Image_", fileName & ".jpg"), ImageFormat.Bmp)
这样,您可以使用短划线替换任何无效字符,无论名称中包含任何不需要的字符,您的文件都将安全保存。