gpt4 book ai didi

c# - 使用lastwritetime从FTP复制文件

转载 作者:行者123 更新时间:2023-12-02 23:17:05 25 4
gpt4 key购买 nike

如何从ftp复制文件并保存原始的lastwritetime而不是当前时间?
此下载文件,并设置lastwritetime当前时间。

$Login = 'anonymous'
$Pass = ''
$url = "ftp://ftp.intel.com/readme.txt"
$local = "C:\docs\readme.txt"

$WC = new-object System.Net.WebClient
$WC.Credentials = [Net.NetworkCredential]::new($Login,$Pass)
$WC.downloadFile($url,$local)

最佳答案

为了设置“服务器上次写入时间”,您需要在检索实际文件的同时进行检索。
两者都完成后,您可以使用服务器中的时间来更新本地上次更新时间。
编辑:我更新了脚本以发出两个请求。第一个很快,因为它只检索远程文件Timestamp。有了它后,我们将文件下载到磁盘并更新文件的最后修改日期。

$LocalFile = "c:\temp\readme2.txt"

$url = "ftp://ftp.intel.com/readme.txt"

#Retrieve the DateTimestamp of the remote file
$WR = [Net.WebRequest]::Create($url)
$WR.Method = [Net.WebRequestMethods+FTP]::GetDateTimestamp
$WR.Credentials = [Net.NetworkCredential]::new("anonymous","")
$Response = $WR.GetResponse()
$RemoteLastModifiedTime = $Response.LastModified

#Retrieve the file content
$WR1 = [Net.WebRequest]::Create($url)
$WR1.Method = [Net.WebRequestMethods+FTP]::DownloadFile
$WR1.Credentials = [Net.NetworkCredential]::new("anonymous","")
$Response1 = $WR1.GetResponse()
$ResponseStream = $Response1.GetResponseStream()
# Create the target file on the local system and the download buffer
$LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create)
[byte[]]$ReadBuffer = New-Object byte[] 1024
# Loop through the download
do {
$ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
$LocalFileFile.Write($ReadBuffer,0,$ReadLength)
}
while ($ReadLength -ne 0)
$LocalFileFile.Close()

#Update the Last Modified DateTime
$(Get-Item $LocalFile).LastWriteTime=$RemoteLastModifiedTime

关于c# - 使用lastwritetime从FTP复制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63414564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com