首页 文章

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

提问于
浏览
85

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

这个字符串有什么问题?

11 回答

  • 0

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

    Path.Combine(str_uploadpath, fileName);
    

    返回一个字符串 .

  • 103

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

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

    如果您尝试将文件保存到文件系统 . 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());
    
  • 2

    除其他可能导致此错误的事项:

    您不能在完整的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.
    

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

  • 1

    尝试改变:

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

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

  • 6

    如果在PowerShell中出现此错误,则's most likely because you'使用 Resolve-Path 来解析远程路径,例如

    Resolve-Path \\server\share\path
    

    在这种情况下, Resolve-Path 返回一个对象,当转换为字符串时,该对象不是't return a valid path. It returns 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
    
  • 0

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

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

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

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

    使用Path.Combine方法有帮助吗?它在连接路径时遇到了问题

  • 0

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

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

    通常,使用提供的API是最佳实践 .

  • 17

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

    这是我快速和肮脏的黑客删除冒号以阻止错误:@ [User :: LocalFile]“ - ”REPLACE((DT_STR,30,1252)GETDATE(),“:”,“ - ”)“ . xml “

  • 2

    对我来说,这个问题是人眼看不到的.841654_ Left-To-Right Embedding字符 .
    在我从Windows文件属性安全选项卡复制粘贴路径之后,它停留在字符串的开头(就在'D'之前) .

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

    所以那些乍一看相同的两条线实际上是不同的 .

相关问题