Showing posts with label Asp.net with Text File. Show all posts
Showing posts with label Asp.net with Text File. Show all posts

Tuesday, February 7, 2012

Display all information of a text file


Get all information from a file

This program will print some of the information regarding file. 
  • Here fi is object of FileInfo class
  • We are calling a constructor with a string filepath.
    protected void Page_Load(object sender, EventArgs e)
    {

        System.IO.FileInfo fi = new FileInfo("D:\\jello.txt");
        Response.Write("Filename:-"+fi.FullName);
        Response.Write("Creation Time:-"+fi.CreationTime);
        Response.Write("File type:-" + fi.GetType);
        Response.Write("Last write time:-" + fi.LastWriteTime);
        Response.Write("File Size:-"+fi.Length);
  
    }


Rename a file in ASP.Net


Rename a file in ASP.Net using C# 

Unfortunately C# does not provide any method for file renaming. The below trick you can use to rename a file.Use the namespace to file handling purpose.
         Using System.io;
  •  Just copy the file with other filename
  •  Then delete the old file
    protected void Page_Load(object sender, EventArgs e)
    {

        string oldf = "D:\\hello.txt";
        string newf = "D:\\jello.txt";

        File.Copy(oldf, newf);
        File.Delete(oldf);

    }

Move file using ASP.Net


Move a file from one location to another

This program will move a file from one location to another.
  • Move() method of file class is used for file movement parpuse.
  • It takes two arguments, source and destination.
  • The File class is located within System.io namespace
  • To move entire directory we can use Move() method of Directory class.
    protected void Page_Load(object sender, EventArgs e)
    {
        string sourceFile = @"C:\Users\Public\public\test.txt";
        string destinationFile = @"C:\Users\Public\private\test.txt";

        // To move a file or folder to a new location:
        System.IO.File.Move(sourceFile, destinationFile);

        // To move an entire directory. To programmatically modify or combine
        // path strings, use the System.IO.Path class.
        System.IO.Directory.Move(@"C:\Users\Public\public\test\", @"C:\Users\Public\private");
    }

Deleting directory using ASP.Net


Deleting a directory using ASP.Net

This program will delete a directory from your system’s hard disk. Include the following namespace to write the program. It will delete all files and subdirectory also.
Using System.IO;
  • Within if condition it is checking wheather directory is exhists or not.
  • Within try block it will delete the directory

protected void Page_Load(object sender, EventArgs e)
    {
        if (System.IO.Directory.Exists("D:\\abc"))
        {
            try
            {
                System.IO.Directory.Delete("D:\\abc", true);
            }

            catch (System.IO.IOException ex)
            {
                Response.Write(ex.Message);
            }
        }

Delete a file from directory using ASP.Net


Delete a file from directory
This program will delete a file from directory.  Add following namespace
Using system.io;
  • At first within if condition it is checking wheather file is exhists or not.
  • Delete method of File class is used for file delete parpuse
            if (File.Exists(fname))
            {
                File.Delete(fname);
                this.Label1.Text = "<font color=green>" + "File deleted" + "</font>";
                show();
            }
            else
            {
                this.Label1.Text = "<font color=green>" + "File not exhists" + "</font>";
            }

Display files in a directory using ASP.Net.


Display files in a directory using ASP.Net.

This program will print all the files within a specific directory.
  •  file is a string array that content all files.
  •  foreach loop will copy each element within fname string.
  • Within for loop each and every file will print.
  
 protected void Page_Load(object sender, EventArgs e)
    {
        string[] file = Directory.GetFiles("D://user//");
        foreach (string fname in file)
        {
           Response.write (fname);
        }
 
    }

Read text File using ASP.Net


Reading of Text file using ASP.Net

The program will read a text file and output will display in screen. I have used StreamReader class to read text file. And rd is the object of StreamReader class.
  • StreamReader class is used to read the content of file.
  • ReadToEnd() method of StreamReader class is used to read file upto last character.
   protected void Page_Load(object sender, EventArgs e)
    {
        StreamReader rd = new StreamReader("D:\\hello.txt");
        string val = rd.ReadToEnd();
        Response.Write(val);
   }

Create a text file using ASP.Net


Create a text file using ASP.Net C#
 
Here I am going to discuss about creation of text file in ASP.Net. I have used StreamWrite class to create the text file. This program will create a hello.txt file in your system’s D drive. You can put your path if you wish. Str is the object of StreamWriter class.
  •    strin is  a object of string class
  •    Write() method of StreamWrite  class is used for writing a string object within a file.
  •     Close() method of StreamWriter class is used for closing the file.
protected void Page_Load(object sender, EventArgs e)
    {

        string strin = "Hello world";
        StreamWriter str = new StreamWriter("D:\\hello.txt",true);
        str.Write(strin);
        str.Close();
       
    }