Web.config:指定文件位置



我对web.config有问题,我是个新手。我不知道如何将文件路径输入btn_uploadFile_Click。这是我的web.config文件路径

  <appSettings>
    <add key="IncidentPath" value="C:NetserveIncident"/>
    <add key="ContractPath" value="C:NetserveContract"/>
  </appSettings>

以及vb代码。我不想使用savePath变量。我想从web.config中检索文件夹路径,然后检查该路径是否有效。这可能吗?如何做到这一点?

Protected Sub btn_uploadFile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_uploadFile.Click
        Try
            Dim savePath As String = "c:NetserveIncident" & Me.in01_txt_incidentNo.Text & ""

            If Not IO.Directory.Exists(savePath) Then
                IO.Directory.CreateDirectory(savePath)
            End If
            If (FileUpload1.HasFile) Then
                ' Get the name of the file to upload.
                Dim fileName As String = FileUpload1.FileName
                While IO.File.Exists(savePath & fileName)
                    fileName = "C" & fileName
                End While

                savePath += fileName
                FileUpload1.SaveAs(savePath)
                saveFiletoDB(savePath, fileName)
            Else
                ' Notify the user that a file was not uploaded.
                ShareVar.dd_error("You did not specify a file to upload.")
            End If
        Catch ex As Exception
            ShareVar.dd_error(ex.Message)
        End Try
    End Sub 
  1. 将对System.Configuration.dll的引用添加到项目中(如果尚未添加),并将Imports System.Configuration添加到代码后面的导入语句中。

  2. 使用ConfigurationManager.AppSettings从.config文件的<appSettings>部分读取值

最新更新