.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

How to rename a file programmatically in c#

Posted By: priya     Posted Date: January 07, 2010    Points:2   Category :ASP.Net
Hi Friends,

How to rename a file programmatically in C# and asp.net? I want to modify the name and append the date in it using program dynamically.

Thanks in advance

Author: Lalij Mer             
Posted Date: January 07, 2010     Points: 5   

Step 1:
Create a new website. Add a web.config file to your website (Right
Click project > Add New Item > Web Configuration File. Keep the
default name as Web.config and click Add).
Step 2: Make the following changes to your web.config

In your <system.web> tag, uncomment the <customError> tag. We will be reading and editing one of the attributes of this tag. The end result after removing the comments should be as shown below:

<authenticationmode="Windows" />


The <customErrors> section enables configuration

of what to do if/when an unhandled error occurs

during the execution of a request. Specifically,

it enables developers to configure html error pages

to be displayed in place of a error stack trace. -->

<customErrorsmode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">

<errorstatusCode="403"redirect="NoAccess.htm" />

<errorstatusCode="404"redirect="FileNotFound.htm" />



Step 3: We will now go ahead and make changes to the 'defaultRedirect' attribute of the <customErrors> tag, using the API we just discussed. For this purpose, add a textbox, label control and a button to the form. After adding the controls, the source view of Default.aspx would look similar to the following:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>



<form id="form1" runat="server">


<asp:Label ID="lblErrorPage" runat="server" Text="Rename Error Page to :"></asp:Label>

<asp:TextBox ID="txtError" runat="server"></asp:TextBox><br />

<br />

<asp:Button ID="btnRename" runat="server" Text="Rename" /></div>




The textbox will be used to display the default value of the 'defaultRedirect' attribute.

Step 4: In the Page_Load, display the current default value of the 'defaultRedirect' attribute. To do so, declare the objects for Configuration and WebConfigurationManager at form level and add the following code at Form_Load():


Configuration configuration;

CustomErrorsSection section;

protected void Page_Load(object sender, EventArgs e)


configuration = WebConfigurationManager.OpenWebConfiguration("~");

section = (CustomErrorsSection)configuration.GetSection("system.web/customErrors");

txtError.Text = section.DefaultRedirect.ToString();


check this article

Please Check my answer if you help....
Thank You...


Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend