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

时间:2023-01-03 09:25:11

I have the following code in my web service:

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

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.

有人能帮我解决这个问题,从代码的第2行错误消息。

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

断点为我提供了str_uploadpath的值,它是C:\\webprojects\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \。

What is wrong with this string?

这个字符串有什么问题?

8 个解决方案

#1


93  

Rather than using str_uploadpath + fileName, try using System.IO.Path.Combine instead:

不要使用str_uploadpath +文件名,尝试使用System.IO.Path。结合:

Path.Combine(str_uploadpath, fileName);

which returns a string.

它返回一个字符串。

#2


35  

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:

我看到发起者发现在尝试保存整个路径的文件名时发生了错误。实际上,在文件名中有一个":"就可以得到这个错误。如果您的文件名中可能有“:”(例如,如果您的文件名中有日期戳),请确保您用其他东西替换它们。即:

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

#3


14  

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:

如果您正在尝试将文件保存到文件系统。路径。如果文件名包含无效字符,那么联合收割机不会提供子弹证明。这里有一个扩展方法,从文件名中删除无效字符:

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

And the usage can be:

用法可以是:

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

#4


6  

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:

例如,这些字符将会破坏StreamWriter函数:

"/"  
":"

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:

可能还有其他特殊的角色也会崩溃。例如,我发现当您尝试将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

One way to prevent this trouble is to replace problem characters in NewFileOutS with benign ones:

防止这种麻烦的一种方法是用无害的方法替换新文件中的问题字符:

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

Hope this saves someone some headaches...!

希望这能让人省心。

#5


2  

If you get this error in PowerShell, it's most likely because you're using Resolve-Path to resolve a remote path, e.g.

如果您在PowerShell中得到这个错误,很可能是因为您正在使用解析路径来解析远程路径,例如。

 Resolve-Path \\server\share\path

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:

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

> [string](Resolve-Path \\server\share\path)
Microsoft.PowerShell.Core\FileSystem::\\server\share\path

The solution is to use the ProviderPath property on the object returned by Resolve-Path:

解决方案是使用resolpath返回的对象上的ProviderPath属性:

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

#6


2  

This was my problem, which may help someone else -- although it wasn't the OP's issue:

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

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

I determined the problem by outputting my path to a log file, and finding it not formatting correctly. Correct for me was quite simply:

我通过将路径输出到日志文件来确定问题,并发现它的格式不正确。对我来说很简单:

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

#7


1  

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

使用的路径。结合方法帮助吗?这是将文件路径连接在一起的更安全的方法。它可能在连接路径上有问题

#8


1  

Try changing:

试着改变:

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

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

to

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

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

#1


93  

Rather than using str_uploadpath + fileName, try using System.IO.Path.Combine instead:

不要使用str_uploadpath +文件名,尝试使用System.IO.Path。结合:

Path.Combine(str_uploadpath, fileName);

which returns a string.

它返回一个字符串。

#2


35  

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:

我看到发起者发现在尝试保存整个路径的文件名时发生了错误。实际上,在文件名中有一个":"就可以得到这个错误。如果您的文件名中可能有“:”(例如,如果您的文件名中有日期戳),请确保您用其他东西替换它们。即:

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

#3


14  

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:

如果您正在尝试将文件保存到文件系统。路径。如果文件名包含无效字符,那么联合收割机不会提供子弹证明。这里有一个扩展方法,从文件名中删除无效字符:

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

And the usage can be:

用法可以是:

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

#4


6  

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:

例如,这些字符将会破坏StreamWriter函数:

"/"  
":"

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:

可能还有其他特殊的角色也会崩溃。例如,我发现当您尝试将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

One way to prevent this trouble is to replace problem characters in NewFileOutS with benign ones:

防止这种麻烦的一种方法是用无害的方法替换新文件中的问题字符:

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

Hope this saves someone some headaches...!

希望这能让人省心。

#5


2  

If you get this error in PowerShell, it's most likely because you're using Resolve-Path to resolve a remote path, e.g.

如果您在PowerShell中得到这个错误,很可能是因为您正在使用解析路径来解析远程路径,例如。

 Resolve-Path \\server\share\path

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:

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

> [string](Resolve-Path \\server\share\path)
Microsoft.PowerShell.Core\FileSystem::\\server\share\path

The solution is to use the ProviderPath property on the object returned by Resolve-Path:

解决方案是使用resolpath返回的对象上的ProviderPath属性:

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

#6


2  

This was my problem, which may help someone else -- although it wasn't the OP's issue:

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

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

I determined the problem by outputting my path to a log file, and finding it not formatting correctly. Correct for me was quite simply:

我通过将路径输出到日志文件来确定问题,并发现它的格式不正确。对我来说很简单:

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

#7


1  

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

使用的路径。结合方法帮助吗?这是将文件路径连接在一起的更安全的方法。它可能在连接路径上有问题

#8


1  

Try changing:

试着改变:

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

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

to

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

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