“不支持给定路径的格式。”

时间:2011-09-08 13:24:03

标签: c# .net webforms upload stream

我的网络服务中有以下代码:

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\\的值。

这个字符串有什么问题?

13 个答案:

答案 0 :(得分:110)

不要使用str_uploadpath + fileName,而是尝试使用System.IO.Path.Combine

Path.Combine(str_uploadpath, fileName);

返回一个字符串。

答案 1 :(得分:40)

我看到发起人发现尝试使用整个路径保存文件名时发生了错误。实际上,在文件名中有":"就足以得到此错误。如果您的文件名中可能有":"(例如,如果您的文件名中有日期戳),请确保将其替换为其他内容。我:

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

答案 2 :(得分:17)

如果您尝试将文件保存到文件系统。 Path.Combine不是防弹,因为如果文件名包含无效字符,它将无法帮助您。这是一个从文件名中删除无效字符的扩展方法:

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

用法可以是:

Path.Combine(str_uploadpath, fileName.ToSafeFileName());

答案 3 :(得分:7)

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

完整的PathFile字符串中不能包含某些字符。

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

"/"  
":"

可能还有其他特殊字符也会崩溃。 我发现当你尝试将DateTime标记放入文件名时会发生这种情况:

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

防止此问题的一种方法是将NewFileOutS中的问题字符替换为良性字符:

' 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.

希望这可以为某些人带来一些麻烦...!

答案 4 :(得分:5)

对我来说,这个问题是人眼看不见的var yourJson = System.IO.File.ReadAllText(@"D:\test\json.txt"); // Works var yourJson = System.IO.File.ReadAllText(@"‪D:\test\json.txt"); // Error Left-To-Right Embedding字符。
在我从Windows文件属性“安全性”选项卡复制粘贴路径之后,它停留在字符串的开头(恰好在“ D”之前)。

.col-md-4:nth-child(3n) {
    padding-right: 0;
}

@media (max-width:768px) {
    .col-md-4 {
        padding-right: 0;
    }
}

乍一看,两行实际上是不同的。

答案 5 :(得分:2)

尝试更改:

Server.MapPath("/UploadBucket/Raw/")

Server.MapPath(@"\UploadBucket\Raw\")

答案 6 :(得分:2)

如果在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属性:

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

答案 7 :(得分:2)

这是我的问题,可能对其他人有所帮助 - 虽然这不是OP的问题:

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

我通过将路径输出到日志文件并发现它没有正确格式化来确定问题。对我而言非常简单:

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

答案 8 :(得分:1)

使用Path.Combine方法有帮助吗?这是一种将文件路径连接在一起的更安全的方法。它可能是在一起加入路径时遇到问题

答案 9 :(得分:0)

我正在使用(有限的)表达式构建器创建变量,该变量可用于简单的文件系统任务中,以在SSIS中创建文件的存档。

这是我快速而肮脏的技巧,用于删除冒号以停止错误: @ [User :: LocalFile] +“-” + REPLACE((DT_STR,30,1252)GETDATE(),“:”,“-”)+“ .xml”

答案 10 :(得分:0)

我今天有同样的问题。 我尝试加载到代码中的文件已打开,可以在Excel中进行编辑。 关闭Excel后,代码开始起作用!

答案 11 :(得分:0)

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

您需要使用指向类的getfullpath。我有同样的错误并已解决...

答案 12 :(得分:-1)

如果该值是文件url(例如file:// C:/),请使用Uri类将其转换为常规文件名:

close

通常,使用提供的API是最佳做法。