I am receiving a System.IO.FileNotFoundException when attempting to access a path like "C:\Path\~somePath\Path\file.ext" it reports the error:

System.IO.FileNotFoundException: Could not find file 'C:\Path\~s'. File name: 'C:\Path\~s' at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
7 Years
Discussion Span
Last Post by serkan sendur

As far as i know, tilde does not mean anything unless you use it at the very beggining of your path, in which case it would refer to your root application folder. I think you should use Server.MapPath instead of ~ .


I am referring to the path directly, and it still seems to have a problem with it. Because of the tilde(~).

The path is being directly referred to by C:\Path\~somePath\Path\file.ext

There is a tilde in the path, it's not in any way meant to refer to it was relative.

Edited by jonturlington: n/a

Private Sub StreamFile(ByVal Q As String)
        Dim iStream As System.IO.Stream

        ' Buffer to read 10K bytes in chunk:
        Dim buffer(10000) As Byte

        ' Length of the file:
        Dim length As Integer

        ' Total bytes to read:
        Dim dataToRead As Long

        ' Identify the file to download including its path.
        Dim filepath As String = Q

        ' Identify the file name.
        Dim filenames As String = System.IO.Path.GetFileName(filepath)
            ' Open the file.
            iStream = New System.IO.FileStream(filepath, System.IO.FileMode.Open, _
                                                   IO.FileAccess.Read, IO.FileShare.Read)

            ' Total bytes to read:
            dataToRead = iStream.Length
            Response.ContentType = "application/octet-stream"
            Response.AddHeader("Content-Disposition", "attachment; filename=" & filenames)

            ' Read the bytes.
            While dataToRead > 0
                ' Verify that the client is connected.
                If Response.IsClientConnected Then
                    ' Read the data in buffer
                    length = iStream.Read(buffer, 0, 10000)

                    ' Write the data to the current output stream.
                    Response.OutputStream.Write(buffer, 0, length)

                    ' Flush the data to the HTML output.

                    ReDim buffer(10000) ' Clear the buffer
                    dataToRead = dataToRead - length
                    'prevent infinite loop if user disconnects
                    dataToRead = -1
                End If
            End While

        Catch ex As Exception
            ' Trap the error, if any.
            Response.Write("Error : " & ex.ToString)
            If IsNothing(iStream) = False Then
                ' Close the file.
            End If
        End Try

    End Sub
    Private Function GetPath(ByVal fileGUID As String) As String
        Dim sqlcommand As SqlClient.SqlCommand = New SqlClient.SqlCommand("sp_getfilename", New SqlClient.SqlConnection("Data Source=;Initial Catalog=;User Id=;Password=;"))
        sqlcommand.CommandType = CommandType.StoredProcedure

        Dim inputParam As SqlClient.SqlParameter = New SqlClient.SqlParameter("@guid", SqlDbType.VarChar, 500)
        inputParam.Direction = ParameterDirection.Input
        inputParam.Value = fileGUID

        Dim outputParam As SqlClient.SqlParameter = New SqlClient.SqlParameter("@returnfilename", SqlDbType.VarChar, 50)
        outputParam.Direction = ParameterDirection.Output

        GetPath = CStr(sqlcommand.Parameters(1).Value).Replace(RepositoryPath, RepositoryUNC)
    End Function

Thank you for making me post the code, it caused me to see the code a little differently and I found the problem. It was in the GetPath function and I was truncating the returned value. Resolved it by switching the length values on the two parameters.


also post how you call StreamFile(ByVal Q As String),
i think your parameter has a problem. and also post the exact path you want to specify.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.