fileinfo_moveto.htm
 
 

//describe copy to function
//wrting to a file
//csc file_stream_moveto.cs
using System;
using System.IO;
using System.Text;
namespace file_stream
{
public class copy_to
{
public static void process(string s1, string s2)
{

//string path = @"C:\MySharp\file_stream\MyTest.txt";
//string path2 = @"C:\MySharp\file_stream\temp\MyTest.txt" + "temp";
string path1 = s1;
string path2 = s2;

FileInfo fi1 = new FileInfo(path1);
FileInfo fi2 = new FileInfo(path2);

try
{
// Create the file and clean up handles.
using (FileStream fs = fi1.Create()) {}

//Ensure that the target does not exist.
fi2.Delete();
//Copy the file.
fi1.MoveTo(path2);
Console.WriteLine("{0} was copied to {1}.", path1, path2);

//Try to copy it again, which should succeed.
//fi1.CopyTo(path2, true);

//Console.WriteLine("Copy operation ");

}
catch
{
Console.WriteLine("Check source and target : double copies are porohibited");
}
}
}

class Test
{
static void Main()
{
string sc = @"f:\mySharp\file_stream\fileinfo_move.txt";
string st = @"f:\mySharp\file_stream\temp\fileinfo_move.txt";
Console.WriteLine("Source and the Target must exists");
Console.WriteLine("Source " + sc);
Console.WriteLine("Target " + st);
Console.WriteLine("Try diff source and target yes or no : " );
string response = Console.ReadLine();
if (response =="yes") {
Console.Write("enter source ");
string s1 = Console.ReadLine();
Console.Write("enter target ");
string s2 = Console.ReadLine();
copy_to.process(s1, s2);
}
else if(response== "no"){ Console.WriteLine( " loading default ");
copy_to.process(sc, st);
}
Console.ReadLine();

}

}
}
 

before exceution