“不支持给定路径的格式。”
我的 Web 服务中有以下代码:
string str_uploadpath = Server.MapPath("/UploadBucket/Raw/");
FileStream objfilestream = new FileStream(str_uploadpath +
fileName, FileMode.Create, FileAccess.ReadWrite);
有人可以帮我解决代码第 2 行中的此错误消息的问题吗?
不支持给定路径的格式。
该文件夹的权限设置为所有人的完全访问权限,这是该文件夹的实际路径。
该断点为我提供了 str_uploadpath
的值为 C:\\webprojects\\webservices\\UploadBucket\\Raw\\
。
这个字符串有什么问题吗?
I have the following code in my web service:
string str_uploadpath = Server.MapPath("/UploadBucket/Raw/");
FileStream objfilestream = new FileStream(str_uploadpath +
fileName, FileMode.Create, FileAccess.ReadWrite);
Can someone help me resolve the issue with this error message from line 2 of the code.
The given path's format is not supported.
Permission on the folder is set to full access to everyone and it is the actual path to the folder.
The breakpoint gave me the value of str_uploadpath
as C:\\webprojects\\webservices\\UploadBucket\\Raw\\
.
What is wrong with this string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
不要使用
str_uploadpath + fileName
,而是尝试使用System.IO.Path .Combine
代替:返回一个字符串。
Rather than using
str_uploadpath + fileName
, try usingSystem.IO.Path.Combine
instead:which returns a string.
我看到发起者发现尝试使用完整路径保存文件名时发生了错误。实际上,文件名中包含
":"
就足以引发此错误。如果您的文件名中可能包含":"
(例如,如果您的文件名中包含日期戳),请确保将其替换为其他内容。 IE:I see that the originator found out that the error occurred when trying to save the filename with an entire path. Actually it's enough to have a
":"
in the file name to get this error. If there might be":"
in your file name (for instance if you have a date stamp in your file name) make sure you replace these with something else. I.e:对我来说,这个问题是人眼看不见的
""
Left-To-右嵌入字符。在我从 Windows 文件属性安全选项卡复制粘贴路径后,它卡在字符串的开头(就在“D”之前)。
因此,乍一看相同的两条线实际上是不同的。
For me the problem was an invisible to human eye
""
Left-To-Right Embedding character.It stuck at the beginning of the string (just before the 'D'), after I copy-pasted the path, from the windows file properties security tab.
So those, identical at first glance, two lines are actually different.
如果您尝试将文件保存到文件系统。 Path.Combine 不是防弹的,因为如果文件名包含无效字符,它不会为您提供帮助。这是一个从文件名中删除无效字符的扩展方法:
用法可以是:
If you are trying to save a file to the file system. Path.Combine is not bullet proof as it won't help you if the file name contains invalid characters. Here is an extension method that strips out invalid characters from file names:
And the usage can be:
可能导致此错误的其他原因包括:
完整的 PathFile 字符串中不能包含某些字符。
例如,这些字符将使 StreamWriter 函数崩溃:
可能还有其他特殊字符也会使其崩溃。
例如,当您尝试将 DateTime 标记放入文件名中时,我发现会发生这种情况:
防止此问题的一种方法是将 NewFileOutS 中的问题字符替换为良性字符:
希望这可以为某人节省一些麻烦...!
Among other things that can cause this error:
You cannot have certain characters in the full PathFile string.
For example, these characters will crash the StreamWriter function:
there may be other special characters that crash it too.
I found this happens when you try, for example, to put a DateTime stamp into a filename:
One way to prevent this trouble is to replace problem characters in NewFileOutS with benign ones:
Hope this saves someone some headaches...!
如果您在 PowerShell 中收到此错误,很可能是因为您使用
Resolve-Path
来解析远程路径,例如在本例中,
Resolve-Path
返回一个对象当转换为字符串时,不会返回有效的路径。它返回 PowerShell 的内部路径:解决方案是在
Resolve-Path
返回的对象上使用ProviderPath
属性:If you get this error in PowerShell, it's most likely because you're using
Resolve-Path
to resolve a remote path, e.g.In this case,
Resolve-Path
returns an object that, when converted to a string, doesn't return a valid path. It returns PowerShell's internal path:The solution is to use the
ProviderPath
property on the object returned byResolve-Path
:尝试将
Server.MapPath("/UploadBucket/Raw/")
更改为
Server.MapPath(@"\UploadBucket\Raw\")
Try changing:
Server.MapPath("/UploadBucket/Raw/")
to
Server.MapPath(@"\UploadBucket\Raw\")
这是我的问题,可能对其他人有帮助——尽管这不是OP的问题:
我通过将路径输出到日志文件来确定问题,并发现它的格式不正确。对我来说正确的很简单:
This was my problem, which may help someone else -- although it wasn't the OP's issue:
I determined the problem by outputting my path to a log file, and finding it not formatting correctly. Correct for me was quite simply:
使用 Path.Combine 方法有帮助吗?这是将文件路径连接在一起的更安全的方法。可能是在将路径连接在一起时遇到问题
Does using the Path.Combine method help? It's a safer way for joining file paths together. It could be that it's having problems joining the paths together
我今天也遇到了同样的问题。
我尝试加载到代码中的文件已打开以在 Excel 中进行编辑。
关闭Excel后,代码开始工作!
I had the same issue today.
The file I was trying to load into my code was open for editing in Excel.
After closing Excel, the code began to work!
我正在使用变量的(有限)表达式构建器在简单的文件系统任务中使用,以在 SSIS 中对文件进行存档。
这是我快速而肮脏的技巧,用于删除冒号以阻止错误:
@[用户::LocalFile] + "-" + REPLACE((DT_STR, 30, 1252) GETDATE(), ":", "-") + ".xml"
I am using the (limited) Expression builder for a Variable for use in a simple File System Task to make an archive of a file in SSIS.
This is my quick and dirty hack to remove the colons to stop the error:
@[User::LocalFile] + "-" + REPLACE((DT_STR, 30, 1252) GETDATE(), ":", "-") + ".xml"
您需要通过指向类获取完整路径。我有同样的错误并修复了...
you need getfullpath by pointed class. I had same error and fixed...
如果值是像 file://C:/whatever 这样的文件 url,请使用 Uri 类转换为常规文件名:
var localPath = (new Uri(urlStylePath)).AbsolutePath
一般来说,使用提供的 API 是最佳实践。
If the value is a file url like file://C:/whatever, use the Uri class to translate to a regular filename:
var localPath = (new Uri(urlStylePath)).AbsolutePath
In general, using the provided API is best practice.