Rename File : File « File Directory « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Rename" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Rename File</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Rename File</h1>
        <asp:Label ID="lblOldName" runat="server" Text="Old Name: "></asp:Label>
        <asp:TextBox ID="txtOldName" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="lblNewName" runat="server" Text="New Name: "></asp:Label>
        <asp:TextBox ID="txtNewName" runat="server"></asp:TextBox><br />
        <br />
        <asp:Button ID="btnBadRename" runat="server" OnClick="btnBadRename_Click" Text="Bad Rename" />
        <asp:Button ID="btnGoodRename" runat="server" OnClick="btnGoodRename_Click" Text="Good Rename" /></div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Rename : System.Web.UI.Page
{
    protected void btnBadRename_Click(object sender, EventArgs e)
    {
        string appPath = Request.PhysicalApplicationPath;

        string oldPath = Path.Combine(appPath, txtOldName.Text);
        string newPath = Path.Combine(appPath, txtNewName.Text);

        File.Move(oldPath, newPath);
    }

    protected void btnGoodRename_Click(object sender, EventArgs e)
    {
        string oldPath = Server.MapPath(txtOldName.Text);
        string newPath = Server.MapPath(txtNewName.Text);

        File.Move(oldPath, newPath);
    }
}








10.1.File
10.1.1.Get file information (VB.net)
10.1.2.Opening a File (C#)
10.1.3.Opening a File (VB)
10.1.4.Delete a file (C#)
10.1.5.Delete a file (VB)
10.1.6.Creating a File (C#)
10.1.7.Creating a File (VB)
10.1.8.Load file content to DropDownList (C#)
10.1.9.Load file content to DropDownList (VB)
10.1.10.Rename File