上传带有日期&的文件时间 [英] Upload a file with date & time
问题描述
大家好
我有一个文件上传代码在下面
Hi all
I have a code to file upload is below
ofdDocToUpload.Filter = "All files (*.*)|*.*"
If ofdDocToUpload.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
Exit Sub
End If
txtReqDocPath.Text = ofdDocToUpload.FileName
给出这样的输出
C:\ Documents and Settings \ superkings \ Desktop \ work on_25Jan2011.txt
但现在的要求是,日期时间应在文件名之前添加,如下所示
F:\ EOSPROJECTS \ MCSAdmin \ ContactMasterFiles \ 2011310155114_TestWord35.docx
我该如何实现,请帮助
which give the output like that
C:\Documents and Settings\superkings\Desktop\work on_25Jan2011.txt
but now requirement is that datetime should be add before the file name like below
F:\EOSPROJECTS\MCSAdmin\ContactMasterFiles\2011310155114_TestWord35.docx
how can I achieve that please help
推荐答案
首先,获取当前日期,然后以格式化的方式为其添加前缀:
现在
First, get the current date, then prefix it in a formatted manner:
Dim now As DateTime = DateTime.Now
txtReqDocPath.Text = now.ToString("yyyyMMddhhmmss_") + ofdDocToUpload.FileName
您可能需要更改格式字符串,我看不到昨天的日期如何获得"2011310155114"!
这里有格式说明符的完整列表:格式设置显示的DateTime-格式字符串说明 [ ^ ]
You may have to change the format string, I can''t see how you get "2011310155114" from yesterdays date!
There is a full list of the format specifiers here: Formatting a DateTime for display - format string description[^]
上传文件时,您仅传输其内容,而不传输任何属性,因此创建时间并且文件的最后修改丢失.您只能使用文件上传时的本地服务器时间.这不是问题:下载时,调用属性System.DateTime.Now
或System.DateTime.UtcNow
,使用所需的区域性将其转换为字符串,并与使用它的文件名连接在一起.您需要使用允许文件名使用字符的格式,不能使用"/"字符(用-"或类似的字符替换它们).
—SA
When you upload a file, you only transfer its contents, not any attributes, so the time of creation and last modification of the file is lost. You can only use the local server''s time at the moment when the file is uploaded. This is not a problem: on download, call the propertySystem.DateTime.Now
orSystem.DateTime.UtcNow
, convert it to string using the culture you want and concatenate with the file name where you use it. You need to use format with characters allowed for file name, no ''/'' characters (replace them with ''-'' or something like that).
—SA
这篇关于上传带有日期&的文件时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!