Click here to Skip to main content
15,905,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,
am uploading a file into application ,like

FileUpload1.SaveAs(Server.MapPath("~/" + FileUpload1.FileName + ""));

"~" tilde indicates the root of a virtual path,i need Files location upon upload is to be obtained from webconfig file

thanks inadvance
Posted

Hi



String Value = ConfigurationManager.AppSettings[Key].tostring();

where Key is the specified in Webconfig .
 
Share this answer
 
Comments
pradeep manne 3-Nov-11 11:05am    
my code is like this public void UploadExcel() {

if (FileUpload1.HasFile) { try {



FileUpload1.SaveAs(Server.MapPath("~/" + FileUpload1.FileName + ""));

LabelUpload.Text = FileUpload1.FileName;

Session["Input_Data_Excel_File"] = Server.MapPath("" + FileUpload1.FileName + " ");

} catch (Exception ex) {

errormsg.Text += "ERROR: " + ex.Message.ToString(); }

} else {

errormsg.Text += "Please select a file to upload.";


}
You may follow below steps to do this.

1) Create a Folder in your Project. For Example with Name - "TestFolder".

2) Define a key in appSettings section of your Web.config file.
XML
<appSettings>

<add key="FileUploadFolder" value="TestFolder" />

</appSettings>

3) Access value of above key as below.
C#
string folderFromConfig = ConfigurationManager.AppSettings["FileUploadFolder"].ToString();

FileUpload1.SaveAs(Server.MapPath(folderFromConfig) + FileUpload1.FileName);
 
Share this answer
 
v2
Comments
pradeep manne 3-Nov-11 11:39am    
Hi Raiskazi am getting the following error
Could not find schema information for the element 'http://schemas.microsoft.com/.NetConfiguration/v2.0:system.serviceM
pradeep manne 3-Nov-11 12:06pm    
Hi raiskazi ,the code actually uploading the file but its not going into TestFolder but the file is storing in application as with renamed file name as TestFolderExcel1.xls
string folder_from_config = ConfigurationManager.AppSettings["FileUploadFolder"].ToString();

FileUpload1.SaveAs(Server.MapPath(folder_from_config) + "\\" + System.IO.Path.GetFileName(FileUpload1.FileName));

<appsettings>

<add key="FileUploadFolder" value="TestFolder" />

</appsettings>
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900