Wednesday, 20 June 2012

How to move the text file or Notepad file or.txt file in VB.NET or C# windows forms

Hi Friends, in this article I will explain about how to move the text file or Notepad file or.txt file in VB.NET or C# windows forms.
I already explained in the previous article How to Create, Read, Write, Copy, Move and Delete a Text File or notepad file using C# and VB.NET

                           For moving the text file we have to using or imports the System.IO namespace. Because the all file classes and methods are located in System.IO namespace. We use the System.IO.File.Move() method to move the text file.
Let we know how to move the text file with the below example.

VB.NET Code:
Imports System.IO
Imports System.Text
Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim FileToMove As String
        Dim MoveFileLocation As String
        FileToMove = "C:\temp\filetomove.txt"
        MoveFileLocation = "C:\temp\Move\filename.txt"

        If System.IO.File.Exists(FileToMove) = True Then
            System.IO.File.Move(FileToMove, MoveFileLocation)
            MessageBox.Show("File Moved Successfully")
        End If
    End Sub
End Class

C# Code:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text;
Public Class Form2

     private void Form2_Load(System.Object sender, System.EventArgs e)
          string FileToMove = null;
          string MoveFileLocation = null;
          FileToMove = "C:\temp\filetomove.txt";
          MoveFileLocation = "C:\temp\Move\filename.txt";

          if (System.IO.File.Exists(FileToMove) == true) {
              System.IO.File.Move(FileToMove, MoveFileLocation);
              MessageBox.Show("File Moved Successfully");
    Public Form2()
          Load += Form2_Load;

                          When we execute the above code the file filetomove.txt located in the temp folder will move to the move folder as filename.txt. If the move folder does not exists then the file will not moved and will arise the 'System.IO.DirectoryNotFoundException’  error.

"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter  on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on  RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog  for more articles"

No comments:

Post a Comment

© 2012-2018 All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.