How to Create a File or Folder IN ASP.NET
Lets see how can we create Files and folder in out computer with the help of the asp.net.
YOU can create the file or folder and even subfolder in any directory of you pc.In this example I have made it possible to enter the path where you want to save the file and it creates the directory as well file.
If you Do Not provide the path then the file will be created at the path specifed(HARD CODED).
SO LETS SEE HOW TO DO IT...................................................................................................
THE CODE DESIGN PAGE:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Create a File or Folder (C#)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="Label1" runat="server" Text="ENTER THE FILE NAME :"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" Display="Dynamic"
ErrorMessage="ENTER ANY FILE NAME" Font-Bold="True" Font-Size="X-Small"
SetFocusOnError="True" ValidationGroup="File">THE FILE CREATED WILL BR IN TXT FORMAT</asp:RequiredFieldValidator><br />
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="STORAGE LOCATION :"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<h5>*NOT NECESSARY( IF PLEASE SPECIFY IN FORMAT MENTIONED:
DIRECTORY:/PARENT_FOLDERNAME/SUBFOLDERNAME) i.e d:/PARENT FOLDERNAME/SUB
FOLDERNAME OR d:/PARENT FOLDERNAME</h5><br />
<br />
<asp:Button ID="Button1" runat="server" Font-Bold="True" ForeColor="#33CC33"
Text="CREATE FILE" ValidationGroup="File" onclick="Button1_Click" />
<br />
<br />
<asp:Label ID="resultlbl" runat="server" Font-Bold="True" Font-Size="Large"
ForeColor="#000066"></asp:Label>
<asp:LinkButton ID="LinkButton1" runat="server" Visible="False">GO TO FILE</asp:LinkButton>
</form>
</body>
</html>
YOU GET THE DESIGN:
THE CODE BEHIND:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string pathString = "";
string pathString2 = "";
// Create a file name for the file you want to create.
//string fileName = System.IO.Path.GetRandomFileName();
// This example uses a random string for the name, but you also can specify
// a particular name.
string fileName = TextBox1.Text.ToUpper() + ".txt";
if (TextBox2.Text.Equals(""))
{
// Specify a name for your PARENT_FOLDER folder.
string folderName = @"D:\MYPARENTFOLDER";
// To create a string that specifies the path to a subfolder under your
// PARENT_FOLDER folder, add a name for the subfolder to folderName.
pathString = System.IO.Path.Combine(folderName, "MYSubFolder");
// You can write out the path name directly instead of using the Combine
// method. Combine just makes the process easier.
pathString2 = @"c:\MyParentFolder\MYSubFolder";
// You can extend the depth of your path if you want to.
//pathString = System.IO.Path.Combine(pathString, "SubSubFolder");
// Create the subfolder. You can verify in File Explorer that you have this
// structure in the C: drive.
// Local Disk (C:)
// Top-Level Folder
// SubFolder
System.IO.Directory.CreateDirectory(pathString);
// Use Combine again to add the file name to the path.
pathString = System.IO.Path.Combine(pathString, fileName);
// Verify the path that you have constructed.
resultlbl.Text = "FILE CREATED AT Path " + pathString;
// Check that the file doesn't already exist. If it doesn't exist, create
// the file and write integers 0 - 99 to it.
// DANGER: System.IO.File.Create will overwrite the file if it already exists.
// This could happen even with random file names, although it is unlikely.
}
else
{
if (TextBox2.Text.Count(x => (x != '/')) > 0)
{
string FULLPATH = TextBox2.Text.ToString();
pathString = FULLPATH;
}
else
{
string FULLPATH = TextBox2.Text.ToString();
pathString =FULLPATH;
}
// You can extend the depth of your path if you want to.
//pathString = System.IO.Path.Combine(pathString, "SubSubFolder");
// Create the subfolder. You can verify in File Explorer that you have this
// structure in the C: drive.
// Local Disk (C:)
// Top-Level Folder
// SubFolder
System.IO.Directory.CreateDirectory(pathString);
// Use Combine again to add the file name to the path.
pathString = System.IO.Path.Combine(pathString, fileName);
// Verify the path that you have constructed.
resultlbl.Text = "FILE CREATED AT Path " + pathString;
}
if (!System.IO.File.Exists(pathString))
{
using (System.IO.FileStream fs = System.IO.File.Create(pathString))
{
for (byte i = 0; i < 100; i++)
{
fs.WriteByte(i);
}
}
}
else
{
resultlbl.Text="File already exists with Filename"+ fileName+" At Location:"+pathString;
LinkButton1.Visible = true;
LinkButton1.PostBackUrl = pathString;
Response.ContentType = @"application/txt"; //here we have selected TXT type file
Response.AddHeader(
@"Content-Disposition",
@"attachment; filename=" + Path.GetFileName(pathString));
Response.WriteFile(pathString);
Response.End();
return;
}
// Read and display the data from your file.
try
{
byte[] readBuffer = System.IO.File.ReadAllBytes(pathString);
foreach (byte b in readBuffer)
{
Console.Write(b + " ");
}
Console.WriteLine();
}
catch (System.IO.IOException ex)
{
Console.WriteLine(ex.Message);
}
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
//System.Console.ReadKey();
}
}
OUTPUT:
SO ITS DONE EASY ............................................
HENCE
YOUR PROBLEM SOLVED..............................
So JUST COPY AND (!!!!UNDERSTAND.......) AND GET
AMAZED ON YOUR INTELLIGENCE
IF ANY ERROR OR SUGGESTION IS ,ALWAYS
WELCOME....................................................................
THANKS FOR BEARING..........................................................................................