簡體   English   中英

“不支持給定路徑的格式。”

[英]“The given path's format is not supported.”

我的網絡服務中有以下代碼:

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

這個字符串有什么問題?

與其使用str_uploadpath + fileNamestr_uploadpath + fileName嘗試使用System.IO.Path.Combine

Path.Combine(str_uploadpath, fileName);

它返回一個字符串。

我看到發起者發現在嘗試使用整個路徑保存文件名時發生了錯誤。 實際上,文件名中有一個":"就足以得到這個錯誤。 如果您的文件名中可能有":" (例如,如果您的文件名中有日期戳),請確保將它們替換為其他內容。 IE:

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

對我來說,問題是人眼不可見的"‪"從左到右嵌入字符”。
在我從 Windows 文件屬性安全選項卡復制粘貼路徑后,它卡在字符串的開頭(就在 'D' 之前)。

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

所以那些乍一看相同的兩條線實際上是不同的。

如果您嘗試將文件保存到文件系統。 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());

可能導致此錯誤的其他因素包括:

完整的 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.

希望這可以為某人節省一些頭痛......!

如果您在 PowerShell 中遇到此錯誤,很可能是因為您正在使用Resolve-Path來解析遠程路徑,例如

 Resolve-Path \\server\share\path

在這種情況下, Resolve-Path返回一個對象,該對象在轉換為字符串時不返回有效路徑。 它返回 PowerShell 的內部路徑:

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

解決方案是在Resolve-Path返回的對象上使用ProviderPath屬性:

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

這是我的問題,這可能對其他人有幫助——盡管這不是 OP 的問題:

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

我通過將我的路徑輸出到日志文件並發現它的格式不正確來確定問題。 對我來說正確的是很簡單:

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

嘗試改變:

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

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

我今天遇到了同樣的問題。 我試圖加載到我的代碼中的文件已打開以在 Excel 中進行編輯。 關閉Excel后,代碼開始工作!

使用Path.Combine方法有幫助嗎? 這是將文件路徑連接在一起的更安全的方法。 可能是將路徑連接在一起時遇到問題

我將(有限的)表達式構建器用於一個簡單的文件系統任務中的變量,以便在 SSIS 中制作文件存檔。

這是我刪除冒號以阻止錯誤的快速而骯臟的技巧:@[User::LocalFile] + "-" + REPLACE((DT_STR, 30, 1252) GETDATE(), ":", "-") + “.xml”

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

你需要通過指向類的 getfullpath 。 我有同樣的錯誤並修復了......

如果該值是文件 url,如 file://C:/whatever,請使用 Uri 類轉換為常規文件名:

var localPath = (new Uri(urlStylePath)).AbsolutePath

通常,使用提供的 API 是最佳實踐。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM