Declare a string in Web.Config file

2019-07-16 13:14发布

问题:

Look at this code piece please:

string dir = "~/parkingTicket/files/"+ID_TextBox.Text;
try
{
   _path = Server.MapPath(dir) + ID_TextBox.Text + "_" + _file.FileName;
   EnsureDirectoriesExist(dir);
   _file.SaveAs(_path);
}

i want to declare (dir) in web.config, how can i do that? thank you.

回答1:

You can do this in appSetting node liket his

<appSettings>
    <add key="dir" value="parkingTicket/files"/>
  </appSettings>

you can read it in code file like

string dir = ConfigurationManager.AppSettings["dir"].ToString();