gpt4 book ai didi

c# - 为什么 File.OpenRead() 将相对路径附加到可执行文件?

转载 作者:太空宇宙 更新时间:2023-11-03 19:39:11 31 4
gpt4 key购买 nike

为什么使用下面的代码

string filePath = @"‪C:\test\upload.pdf"
FileStream fs = File.OpenRead(filePath);

引发以下异常?

The filename, directory name, or volume label syntax is incorrect : 
'C:\ConsoleApp\bin\Debug\netcoreapp2.1\‪‪C:\test\upload.pdf'

C:\ConsoleApp\bin\Debug\netcoreapp2.1\ 目录来自哪里?

更新:在我的例子中,File.OpenRead() 存在于一个 dll 和 filePath (C:\test\upload.pdf) 中通过使用 dll 的应用程序发送。

最佳答案

该字符串以不可见字符开头,因此它不是有效路径。这个

(int)@"‪C:\test\upload.pdf"[0]

返回

8234

或十六进制 202A。那是 LEFT-TO-RIGHT EMBEDDING标点符号

更新

Raymond Chen 发表文章Why is there an invisible U+202A at the start of my file name? .

We saw some time ago that you can, as a last resort, insert the character U+202B (RIGHT-TO-LEFT EMBEDDING) to force text to be interpreted as right-to-left. The converse character is U+202A (LEFT-TO-RIGHT EMBEDDING), which forces text to be interpreted as left-to-right.

The Security dialog box inserts that control character in the file name field in order to ensure that the path components are interpreted in the expected manner. Unfortunately, it also means that if you try to copy the text out of the dialog box, the Unicode formatting control character comes along for a ride. Since the character is normally invisible, it can create all sorts of silent confusion.

(We’re lucky that the confusion was quickly detected by Notepad and the command prompt. But imagine if you had pasted the path into the source code to a C program!)

在那篇文章记事本获得 UTF8 支持后的 4 年里,字符没有被问号替换。粘贴到具有不完整 UTF8 支持的当前 Windows 控制台仍会替换该字符。

关于c# - 为什么 File.OpenRead() 将相对路径附加到可执行文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56355472/

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