Search code examples
.netvb.netsave-as

SaveAs method is configured to require a rooted path -- help understanding existing code block


I have written a function to upload data from an excel spreadsheet to a form. Testing from my computer was successful. I migrated my vb.net code to the dev server and now I'm getting a rooted path message.

My code works with existing code written by someone else. I don't quite understand exactly what it is doing because there's no comments and I'm new to programming.

My thought is the first section of code is looking for the path of the file being submitted by the user (the IF section) and the second section of code (in the ELSE section) is -- not really sure actually as the code seems redundant. I do know we have a temp folder on the server. It would be helpful to understand what the code is doing so I can figure out where to put the server path. Can someone comment on the code to help me understand?

If WebPath.Contains("localhost") Then
    FilePath = Path.Combine("c:\open", FileName)
    FileUpload1.SaveAs(FilePath)
Else
    Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
    FilePath = FolderPath & FileName
    FileUpload1.SaveAs(FilePath)
End If

Solution

  • From my understanding:

    ' localhost usually refers to development environment
    If WebPath.Contains("localhost") Then
        FilePath = Path.Combine("c:\open", FileName)
        FileUpload1.SaveAs(FilePath)
    ' So if it is not localhost, the code will goes here
    Else
        ' The code is trying to grab the FolderPath value from the .config file
        ' For example: web.config file
        ' Here is the example of how it may looks inside the web.config file
        ' <?xml version="1.0" encoding="utf-8" ?>
        '  <configuration>
        '   <appSettings>
        '    <add key="FolderPath" value="filepath"/>
        '   </appSettings>
        '  </configuration>
        ' So, if you want to change the location, change the "filepath" value in the web.config file
        Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
        ' Also use Path.Combine over here
        FilePath = Path.Combine(FolderPath,FileName)
        FileUpload1.SaveAs(FilePath)
    End If