Copying Data from a file

copying data from file

Page 1 of 1

2 Replies - 620 Views - Last Post: 24 November 2009 - 09:40 AM Rate Topic: -----

#1 afridi2129  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 04-November 07

Copying Data from a file

Posted 24 November 2009 - 09:07 AM

Hello.. see the below code. currently this code remove // from any file and save it to another temp file ..now problem is how can i remove the old file and rename the temp file to the origional file...

 using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace Program1
{
	class Program
	{
		static void Main(string[] args)
		{
			string filepath;
			string filename;
			string line = null;
			//Enter the path for file
			Console.Write(@"Enter the Path of File:");
			filepath = Console.ReadLine().ToString();

			//Enter the File name
			Console.Write("Enter the File Name:");
			filename = Console.ReadLine();

			using (StreamReader Reader = new StreamReader(filepath + filename))
			{
				using (StreamWriter write = new StreamWriter(filepath +"_"+filename))//_ mean a temp file in the same directory
				{
					while ((line = Reader.ReadLine()) != null)
					{
						if (line.Contains("//"))
						{
							int i = line.IndexOf(line);//this will see for the starting index of current line 
							line.Remove(i);	  //this will remove from starting index to the last of the line
						}
						else

						write.WriteLine(line);//this will save the remaining data to temp file..else part
						
					}
				}
			}
			
			Console.WriteLine("Press any key to exit");
			Console.ReadKey();
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Copying Data from a file

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Copying Data from a file

Posted 24 November 2009 - 09:33 AM

File.Move(tempFile, otherFile);
?
Was This Post Helpful? 0
  • +
  • -

#3 afridi2129  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 04-November 07

Re: Copying Data from a file

Posted 24 November 2009 - 09:40 AM

thanx for reply.. but in the mean time i have done the problem with this ..

  File.Delete(filepath + filename);
			File.Copy(filepath + "_" + filename, filepath + filename);
			File.Delete(filepath + "_" + filename);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1