C# &引用;给定路径';不支持s格式;

C# &引用;给定路径';不支持s格式;,c#,.net,webforms,upload,stream,C#,.net,Webforms,Upload,Stream,我的web服务中有以下代码: string str_uploadpath = Server.MapPath("/UploadBucket/Raw/"); FileStream objfilestream = new FileStream(str_uploadpath + fileName, FileMode.Create, FileAccess.ReadWrite); 有人能帮我解决代码第2行的错误消息的问题吗 不支持给定路径的格式 对文件夹的权限设置为“对所有

我的web服务中有以下代码:

string str_uploadpath = Server.MapPath("/UploadBucket/Raw/");
FileStream objfilestream = new FileStream(str_uploadpath +
                fileName, FileMode.Create, FileAccess.ReadWrite);
有人能帮我解决代码第2行的错误消息的问题吗

不支持给定路径的格式

对文件夹的权限设置为“对所有人的完全访问”,这是文件夹的实际路径

断点给了我
str\u uploadpath
的值,作为
C:\\webprojects\\webservices\\UploadBucket\\Raw\\


此字符串有什么问题?

请尝试使用以下命令,而不是使用
str\u uploadpath+fileName

返回字符串。

使用该方法是否有帮助?这是将文件路径连接在一起的更安全的方法。可能是将路径连接在一起时遇到问题

请尝试更改:

Server.MapPath(“/UploadBucket/Raw/”)


Server.MapPath(@“\UploadBucket\Raw\”)

我发现发起者在尝试使用整个路径保存文件名时发现了错误。实际上,在文件名中有一个
:“
就足以得到这个错误。如果文件名中可能有
:“
(例如,如果文件名中有日期戳),请确保用其他内容替换这些内容。即:

string fullFileName = fileName.Split('.')[0] + "(" + DateTime.Now.ToString().Replace(':', '-') + ")." + fileName.Split('.')[1];

如果您在PowerShell中遇到此错误,很可能是因为您正在使用
Resolve Path
解析远程路径,例如:

 Resolve-Path \\server\share\path
在本例中,
Resolve Path
返回一个对象,该对象在转换为字符串时不会返回有效路径。它返回PowerShell的内部路径:

> [string](Resolve-Path \\server\share\path)
Microsoft.PowerShell.Core\FileSystem::\\server\share\path
解决方案是对解析路径返回的对象使用
ProviderPath
属性:

> Resolve-Path \\server\share\path | Select-Object -ExpandProperty PRoviderPath
\\server\share\path
> (Resolve-Path \\server\share\path).ProviderPath
\\server\share\path

除其他可能导致此错误的因素外:

完整路径文件字符串中不能包含某些字符

例如,这些字符将使StreamWriter函数崩溃:

"/"  
":"
可能还有其他特殊字符也会使它崩溃。 例如,当您尝试将日期时间戳放入文件名时,我发现会发生这种情况:

AppPath = Path.GetDirectoryName(giFileNames(0))  
' AppPath is a valid path from system. (This was easy in VB6, just AppPath = App.Path & "\")
' AppPath must have "\" char at the end...

DateTime = DateAndTime.Now.ToString ' fails StreamWriter... has ":" characters
FileOut = "Data_Summary_" & DateTime & ".dat"
NewFileOutS = Path.Combine(AppPath, FileOut)
Using sw As StreamWriter = New StreamWriter(NewFileOutS  , True) ' true to append
        sw.WriteLine(NewFileOutS)
        sw.Dispose()
    End Using
防止此问题的一种方法是将新文件输出中的问题字符替换为良性字符:

' clean the File output file string NewFileOutS so StreamWriter will work
 NewFileOutS = NewFileOutS.Replace("/","-") ' replace / with -
 NewFileOutS = NewFileOutS.Replace(":","-") ' replace : with - 

' after cleaning the FileNamePath string NewFileOutS, StreamWriter will not throw an (Unhandled) exception.

希望这能帮你省去一些麻烦

如果您试图将文件保存到文件系统。Combine不是防弹的,因为如果文件名包含无效字符,它对您没有帮助。以下是从文件名中删除无效字符的扩展名方法:

public static string ToSafeFileName(this string s)
{
        return s
            .Replace("\\", "")
            .Replace("/", "")
            .Replace("\"", "")
            .Replace("*", "")
            .Replace(":", "")
            .Replace("?", "")
            .Replace("<", "")
            .Replace(">", "")
            .Replace("|", "");
    }

这是我的问题,可能会帮助其他人——尽管这不是OP的问题:

DirectoryInfo diTemp = new DirectoryInfo(strSomePath);
FileStream fsTemp = new FileStream(diTemp.ToString());
我通过将路径输出到日志文件并发现其格式不正确来确定问题。对我来说,正确的答案很简单:

DirectoryInfo diTemp = new DirectoryInfo(strSomePath);
FileStream fsTemp = new FileStream(diTemp.FullName.ToString());

如果该值是file://C:/where之类的文件url,请使用Uri类转换为常规文件名:

AppPath = Path.GetDirectoryName(giFileNames(0))  
' AppPath is a valid path from system. (This was easy in VB6, just AppPath = App.Path & "\")
' AppPath must have "\" char at the end...

DateTime = DateAndTime.Now.ToString ' fails StreamWriter... has ":" characters
FileOut = "Data_Summary_" & DateTime & ".dat"
NewFileOutS = Path.Combine(AppPath, FileOut)
Using sw As StreamWriter = New StreamWriter(NewFileOutS  , True) ' true to append
        sw.WriteLine(NewFileOutS)
        sw.Dispose()
    End Using
var localPath=(新Uri(urlStylePath)).AbsolutePath

一般来说,使用提供的API是最佳实践。

我使用(有限的)表达式生成器作为变量,用于在简单的文件系统任务中对SSIS中的文件进行归档

这是我删除冒号以阻止错误的快速而肮脏的攻击:
@[User::LocalFile]+“-”+REPLACE((DT_STR,301252)GETDATE(),“:”,“-”+”.xml“

对我来说,问题是肉眼看不见的
”‪"字符。
在我从windows文件属性安全选项卡复制粘贴路径后,它卡在字符串的开头(就在“D”之前)

var yourJson = System.IO.File.ReadAllText(@"D:\test\json.txt"); // Works
var yourJson = System.IO.File.ReadAllText(@"‪D:\test\json.txt"); // Error

因此,乍一看,这两行是相同的,实际上是不同的。

我今天也有同样的问题。 我试图加载到代码中的文件已打开,可以在Excel中编辑。 关闭Excel后,代码开始工作

Image img = Image.FromFile(System.IO.Path.GetFullPath("C:\\ File Address"));

你需要按指定的类获取完整路径。我也有同样的错误,并修复了…

URL通常有前斜杠,而且
MapPath
足够聪明,可以用任何一种方式来解决它。@spender在字符串的前面有一个
@
可以转义这些内容。
fileName
的值是多少?听起来像
fileName
是空的。Justin,你说得对。文件名中的值在名称中有C:。这让我很难受。谢谢。现在出现错误:错误4 A using namespace指令只能应用于名称空间;'System.IO.Path'是一种类型,而不是namespace@All使用System.IO;
放在上面,然后清除
stru uploadpath+fileName
并写入
Path.Combine(str_uploadpath,fileName)
现在代码如下所示,相同的错误:不支持给定路径的格式。字符串str_uploadpath=Server.MapPath(@/UploadBucket/Raw/);str_uploadpath=Path.Combine(str_uploadpath,fileName);FileStream objfilestream=newfilestream(str_uploadpath,FileMode.Create,FileAccess.ReadWrite);@所有人都尝试调试代码。在导致错误的行的正前方放置一个断点(Visual Studio中的F9)。运行您的程序。当程序在断点处停止时,将鼠标悬停在变量
str_uploadpath
。它的值是什么?我得到的路径在以下路径上不受支持,为什么?“C:\Users\Admin\AppData\Local\Adobe\Flash CS6\en\u US\Configuration\CodeModel\cm cache\SwcCache\basemovie3.swc1273593\library.swf“如果我将其粘贴到资源管理器中,它会正常打开,但.NET的System.IO.File.ReadAllBytes方法会引发该错误。这肯定是一个有效且准确的路径,为什么会出现错误?这修复了我的文件名一直存在的问题。我将当前日期和时间附加到文件中,并且:”值导致我的程序抛出引用的错误OP。这通常是通过使用
Path.GetInvalidPathChars
而不是
Path.GetInvalidFileNameChars
,就像我一样。谢谢!这就是为什么发布多个答案总是一个好主意的原因。啊,谢谢!我正在保存一个名称中包含ISO日期字符串的文件,但是包含非法“:”!Thank!或更短的
返回字符串.Concat
Image img = Image.FromFile(System.IO.Path.GetFullPath("C:\\ File Address"));