在Microsoft的博客文章中,他们说明了如何编写URI以指定本地系统文件路径。
共享网络共享文件的路径时,某些聊天程序将在浏览器中打开这些文件。
因此,我手动编写了将Windows路径转换为文件URI所需的更改的代码
UNC Windows path: \\sharepoint.business.com\DavWWWRoot\rs\project 1\document.txt
变成
URI: file://sharepoint.business.com/DavWWWRoot/rs/project%201/document.txt
我每次都讨厌手工编码,并且想知道是否有一种快速转换为文件URI的方法。
我的计算机没有管理员权限,因此无法安装软件。
PowerShell是一种自动完成上述繁琐的重复任务的绝妙方法!
使用PowerShell(所有版本),将上述UNC路径转换为文件URI非常简单,并且仅需要format和replace运算符,例如:
$Path = "\\sharepoint.business.com\DavWWWRoot\rs\project 1\document.txt"
# replace back slash characters with a forward slash, url-encode spaces,
# and then prepend "file:" to the resulting string
# note: the "\\" in the first use of the replace operator is an escaped
# (single) back slash, and resembles the leading "\\" in the UNC path
# by coincidence only
"file:{0}" -f ($Path -replace "\\", "/" -replace " ", "%20")
结果如下:
file://sharepoint.business.com/DavWWWRoot/rs/project%201/document.txt
最后,应尽可能将上述重复执行的任务纳入PowerShell函数。这样可以节省将来的时间,并确保始终以完全相同的方式执行每个任务。
以下功能与上述功能等效:
function ConvertTo-FileUri {
param (
[Parameter(Mandatory)]
[string]
$Path
)
$SanitizedPath = $Path -replace "\\", "/" -replace " ", "%20"
"file:{0}" -f $SanitizedPath
}
定义函数(并将其加载到当前的PowerShell会话中)后,只需按名称调用该函数,并提供要转换为参数的UNC路径即可,例如:
ConvertTo-FileUri -Path "\\sharepoint.business.com\DavWWWRoot\rs\project 1\document.txt"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句