Initial commit

This commit is contained in:
Michael Reber
2019-11-15 12:59:38 +01:00
parent 40a414d210
commit b880c3ccde
6814 changed files with 379441 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
/*
* C# Program to Calculate the Size of Folder
*/
using System;
using System.Linq;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo dInfo = new DirectoryInfo(@"C:/sri");
long sizeOfDir = DirectorySize(dInfo, true);
Console.WriteLine("Directory size in Bytes : " +
"{0:N0} Bytes", sizeOfDir);
Console.WriteLine("Directory size in KB : " +
"{0:N2} KB", ((double)sizeOfDir) / 1024);
Console.WriteLine("Directory size in MB : " +
"{0:N2} MB", ((double)sizeOfDir) / (1024 * 1024));
Console.ReadLine();
}
static long DirectorySize(DirectoryInfo dInfo, bool includeSubDir)
{
long totalSize = dInfo.EnumerateFiles()
.Sum(file => file.Length);
if (includeSubDir)
{
totalSize += dInfo.EnumerateDirectories()
.Sum(dir => DirectorySize(dir, true));
}
return totalSize;
}
}
}
/*
Directory Size in Bytes : 1,482 Bytes
Directory Size in KB : 1.45 KB
Directory Size in MB : 0.00 MB

View File

@@ -0,0 +1,25 @@
/*
* C# Program to Check the Existence of a File
*/
using System;
using System.IO;
class Program
{
static void Main()
{
FileInfo info = new FileInfo("C:\\sri\\srip.txt");
bool exists = info.Exists;
if (exists == true)
{
Console.WriteLine("The File Exists");
}
else
{
Console.WriteLine("No Such File Found");
}
Console.Read();
}
}
/*
File Exists

View File

@@ -0,0 +1,19 @@
/*
* C# Program to Print the Sum of all the Multiples of 3 and 5
*/
using System;
using System.IO;
class Program
{
static void Main()
{
File.Copy("sri.txt", "srip.txt");
Console.WriteLine(File.ReadAllText("sri.txt"));
Console.WriteLine(File.ReadAllText("srip.txt"));
Console.Read();
}
}
/*
Contents of File S
Contents of File S

View File

@@ -0,0 +1,17 @@
/*
* C# Program to Create a Directory
*/
using System;
using System.IO;
class program
{
public static void Main()
{
Directory.CreateDirectory("C:\\NewDirectory");
Console.WriteLine("NewDirectory is Created in C Directory");
Console.ReadLine();
}
}
/*
NewDirectory is Created in C Directory

View File

@@ -0,0 +1,30 @@
/*
* C# Program to Create a File
*/
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string textpath = @"c:\sri\test.txt";
using (FileStream fs = File.Create(textpath))
{
Byte[] info = new UTF8Encoding(true).GetBytes("File is Created");
fs.Write(info, 0, info.Length);
}
using (StreamReader sr = File.OpenText(textpath))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
Console.Read();
}
}
/*
File is Created

View File

@@ -0,0 +1,31 @@
/*
* C# Program to Demonstrate StringReader
*/
using System;
using System.IO;
class Program
{
const string text = @"Sanfoundry
offers Training and Competency
development programs";
static void Main()
{
using (StringReader reader = new StringReader(text))
{
int count = 0;
string textline;
while ((textline = reader.ReadLine()) != null)
{
count++;
Console.WriteLine("Line {0}: {1}", count, textline);
}
Console.ReadLine();
}
}
}
/*
Line 1 : Sanfoundry
Line 2 : Offers Training and Competency
Line 3 : development programs

View File

@@ -0,0 +1,23 @@
/*
* C# Program to Get Content from a File and Read the Content 1 Byte at a Time
*/
using System;
using System.IO;
public sealed class Program
{
public static void Main()
{
using (Stream s = new FileStream(@"c:\sri\srip.txt", FileMode.Open))
{
int read;
while ((read = s.ReadByte()) != -1)
{
Console.Write("{0} ", read);
}
Console.ReadLine();
}
}
}
/*
71 79 79 68 77 79 82 78 73 78 71

View File

@@ -0,0 +1,21 @@
/*
* C# Program to Get File Time using File Class
*/
using System;
using System.IO;
class Program
{
static void Main()
{
FileInfo info = new FileInfo("C:\\srip.txt");
DateTime time = info.CreationTime;
Console.WriteLine("File was Created at : ");
Console.Write(time);
Console.Read();
}
}
/*
File was Created at :
9/30/2013 12:15:44 PM

View File

@@ -0,0 +1,46 @@
/*
* C# Program to Illustrate Memory Stream Class
*/
using System;
using System.IO;
using System.Text;
class MemStream
{
static void Main()
{
int count;
byte[] byteArray;
char[] charArray;
UnicodeEncoding uniEncoding = new UnicodeEncoding();
byte[] firstString = uniEncoding.GetBytes("Invalid file path characters are: ");
byte[] secondString = uniEncoding.GetBytes(Path.GetInvalidPathChars());
using(MemoryStream memStream = new MemoryStream(100))
{
memStream.Write(firstString, 0, firstString.Length);
count = 0;
while(count < secondString.Length)
{
memStream.WriteByte(secondString[count++]);
}
Console.WriteLine("Capacity = {0}, Length = {1}, Position = {2}\n",
memStream.Capacity.ToString(),
memStream.Length.ToString(),
memStream.Position.ToString());
memStream.Seek(0, SeekOrigin.Begin);
byteArray = new byte[memStream.Length];
count = memStream.Read(byteArray, 0, 20);
while (count < memStream.Length)
{
byteArray[count++] = Convert.ToByte(memStream.ReadByte());
}
charArray = new char[uniEncoding.GetCharCount(byteArray, 0, count)];
uniEncoding.GetDecoder().GetChars(byteArray, 0, count, charArray, 0);
Console.WriteLine(charArray);
Console.Read();
}
}
}
/*
Capacity = 256 Length = 140 Position =140
Invalid File Path Characters are : "<>|

View File

@@ -0,0 +1,49 @@
/*
* C# Program to Illustrate Methods of FileInfo Class
*/
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = Path.GetTempFileName();
FileInfo fi1 = new FileInfo(path);
using (StreamWriter sw = fi1.CreateText())
{
sw.WriteLine("This is");
sw.WriteLine("Codenza");
sw.WriteLine("Website");
}
using (StreamReader sr = fi1.OpenText())
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
try
{
string path2 = Path.GetTempFileName();
FileInfo fi2 = new FileInfo(path2);
fi2.Delete();
fi1.CopyTo(path2);
Console.WriteLine("{0} was copied to {1}.", path, path2);
fi2.Delete();
Console.WriteLine("{0} was successfully deleted.", path2);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
Console.Read();
}
}
/*
This is
Codenza
Website.
C:\Users\win7\AppData\Local\Temp\tmpAEF8.tmp was copied to C:\users\Win7\AppData\Local\Temp\tmpAEF7.tmp
C:\users\Win7\AppData\Local\Temp\tmpAEF8.tmp was successfully deleted.

View File

@@ -0,0 +1,25 @@
/*
* C# Program to Illustrate StringBuilder
*/
using System;
using System.Text;
class Program
{
static void Main()
{
StringBuilder bd = new StringBuilder();
bd.Append("1 ");
bd.Append("2 ");
bd.Append("3 ");
for (int i = 0; i < 5; i++)
{
bd.Append("z ");
}
string result = bd.ToString();
Console.WriteLine(result);
Console.ReadLine();
}
}
/*
1 2 3 z z z z z

View File

@@ -0,0 +1,44 @@
/*
* C# Program to Illustrate StringWriter
*/
using System;
using System.IO;
using System.Text;
public class stringwrt
{
StringBuilder sb = new StringBuilder();
public stringwrt()
{
Writer();
}
public static void Main()
{
stringwrt srw = new stringwrt();
}
private void Writer()
{
StringWriter sw = new StringWriter(sb);
Console.WriteLine("STUDENT DETAILS : ");
Console.Write("Name :");
string name = Console.ReadLine();
sw.WriteLine("Name :" + name);
Console.Write("Department :");
string Department = Console.ReadLine();
sw.WriteLine("Department :" + Department);
Console.Write("College Name :");
string CollegeName = Console.ReadLine();
sw.WriteLine("College Name :" + CollegeName);
Console.WriteLine("Information Saved!");
Console.WriteLine();
sw.Flush();
sw.Close();
Console.ReadLine();
}
}
/*
STUDENT DETAILS :
Name : BOB
Department : IT
College Name : NIIT
Information Saved!

View File

@@ -0,0 +1,43 @@
/*
* C# Program to Implement BinaryReader
*/
using System;
using System.IO;
class ConsoleApplication
{
const string fileName = "program.dat";
static void Main()
{
Write();
Console.WriteLine("Using Binary Writer Class the Contents are Written ");
Display();
}
public static void Write()
{
using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
writer.Write(1.250F);
writer.Write(@"C:\Temp");
}
}
public static void Display()
{
float aspectRatio;
string tempDirectory;
if (File.Exists(fileName))
{
using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
{
aspectRatio = reader.ReadSingle();
tempDirectory = reader.ReadString();
}
Console.WriteLine("Aspect Ratio Set to : " + aspectRatio);
Console.WriteLine("Temp Directory is : " + tempDirectory);
Console.Read();
}
}
}
/*
Using Binary Writer Class the Contents are Written
Aspect Ratio set to : 1.25
Temp Directory is : C:\Temp

View File

@@ -0,0 +1,27 @@
/*
* C# Program to List Disk Drives
*/
using System;
using System.IO;
class Test
{
public static void Main()
{
DriveInfo[] driverslist = DriveInfo.GetDrives();
foreach (DriveInfo d in driverslist)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" File type: {0}", d.DriveType);
if (d.IsReady == true)
{
Console.WriteLine(" Total size of drive:{0, 15} bytes ",d.TotalSize);
Console.Read();
}
}
}
}
/*
Drive C:\
File Type : Fixed
Total Size of Drive : 107268272128

View File

@@ -0,0 +1,25 @@
/*
* C# Program to List the Files in a Directory
*/
using System;
using System.IO;
class Program
{
static void Main()
{
string[] array1 = Directory.GetFiles(@"D:\");
Console.WriteLine("Files in the Directory");
foreach (string name in array1)
{
Console.WriteLine(name);
}
Console.Read();
}
}
/*
Files in the Directory
D:\demo1.cs
D:\demo1.exe
D:\msdia80.dll
D:\demo1.txt

View File

@@ -0,0 +1,66 @@
/*
* C# Program to Perform File Comparison
*/
using System;
using System.Threading;
using System.IO;
class Reader
{
string fileName;
public string data;
public Reader(string fn)
{
fileName = fn;
}
public void Read()
{
FileStream s = new FileStream(fileName, FileMode.Open);
StreamReader r = new StreamReader(s);
data = r.ReadToEnd();
r.Close();
s.Close();
}
}
class ThreadSample
{
static void Main(string[] arg)
{
if (arg.Length == 2)
{
Reader a = new Reader(arg[0]);
Reader b = new Reader(arg[1]);
Thread ta = new Thread(new ThreadStart(a.Read));
Thread tb = new Thread(new ThreadStart(b.Read));
ta.Start();
tb.Start();
ta.Join();
tb.Join();
if (a.data.Length == b.data.Length)
{
int i = 0;
while (i < a.data.Length && a.data[i] == b.data[i]) i++;
if (i == a.data.Length)
Console.WriteLine("Files {0} and {1} are equal", arg[0], arg[1]);
else
Console.WriteLine("Files {0} and {1} are not equal", arg[0], arg[1]);
}
else
{
Console.WriteLine("Files {0} and {1} are not equal", arg[0], arg[1]);
}
}
else
{
Console.WriteLine("-- enter two file names");
}
Console.ReadLine();
}
}
/*
D:\Desktop\c#\program codes>pgno382.exe d:\\sri\\File1.txt d:\\sri\\File1.txt
Files d:\\sri\\File1.txt and d:\\sri\\File1.txt
are equal

View File

@@ -0,0 +1,29 @@
/*
* C# Program to Perform Text Operations in a File
*/
using System;
using System.IO;
class Program
{
static void Main()
{
FileInfo finfo = new FileInfo("C:\\sri\\srip.txt");
using (StreamWriter writer = finfo.AppendText())
{
writer.WriteLine("New File with various Text operations");
}
finfo = new FileInfo("C:\\sri\\srip.txt");
using (StreamWriter writer = finfo.CreateText())
{
writer.WriteLine("New File with various Text operations");
}
using (StreamReader reader = finfo.OpenText())
{
Console.WriteLine(reader.ReadToEnd());
}
Console.Read();
}
}
/*
New File with various Text operations

View File

@@ -0,0 +1,23 @@
/*
* C# Program to Read Data from Stream and Cast Data to Chars
*/
using System;
using System.IO;
public sealed class Program
{
public static void Main()
{
using (Stream s = new FileStream(@"c:\sri\srip.txt", FileMode.Open))
{
int read;
while ((read = s.ReadByte()) != -1)
{
Console.Write("{0} ", (char)read);
}
Console.ReadLine();
}
}
}
/*
G O O D M O R N I N G

View File

@@ -0,0 +1,44 @@
/*
* C# Program to Read Lines from a File until the End of File is Reached
*/
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\sri\srip.txt";
try
{
if (File.Exists(path))
{
File.Delete(path);
}
using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine("This");
sw.WriteLine("text is");
sw.WriteLine("to test");
sw.WriteLine("Reading");
}
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
Console.WriteLine(sr.ReadLine());
}
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
Console.Read();
}
}
/*
This
text is
to test
reading

View File

@@ -0,0 +1,31 @@
/*
* C# Program to Read Contents of a File
*/
using System;
using System.IO;
class FileRead
{
public void readdata()
{
FileStream fs = new FileStream("Myfile.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);//Position the File Pointer at the Beginning of the File
sr.BaseStream.Seek(0, SeekOrigin.Begin);//Read till the End of the File is Encountered
string str = sr.ReadLine();
while (str != null)
{
Console.WriteLine("{0}", str);
str = sr.ReadLine();
}
//Close the Writer and File
sr.Close();
fs.Close();
}
public static void Main(String[] args)
{
FileRead fr = new FileRead();
fr.readdata();
}
}
/*
The text which your are reading are read from the file named myfile.txt that is created already.

View File

@@ -0,0 +1,23 @@
/*
* C# Program to Search Directories and List Files
*/
using System;
using System.IO;
class Program
{
static void Main()
{
string[] Dirfile = Directory.GetFiles("C:\\sri\\","*.*",SearchOption.AllDirectories);
foreach (string file in Dirfile)
{
Console.WriteLine(file);
}
Console.Read();
}
}
/*
The List of Files in the Directory are :
C:\sri\message.txt
C:\sri\srip.txt
C:\sri\test.txt

View File

@@ -0,0 +1,33 @@
/*
* C# Program to Trap Events from File
*/
using System;
using System.IO;
class Test
{
static void namechang(object sender, RenamedEventArgs evn)
{
Console.WriteLine("{0} NameChanged to {1}", evn.OldFullPath, evn.FullPath);
}
static void changed(object sender, FileSystemEventArgs evn)
{
Console.WriteLine(evn.FullPath + " " + evn.ChangeType);
}
static void Main(string[] arg)
{
FileSystemWatcher w = new FileSystemWatcher();
w.Path = "d:\\srip";
w.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName |NotifyFilters.LastAccess | NotifyFilters.LastWrite;
w.Filter = "";
w.Created += new FileSystemEventHandler(changed);
w.Deleted += new FileSystemEventHandler(changed);
w.Changed += new FileSystemEventHandler(changed);
w.Renamed += new RenamedEventHandler(namechang);
w.EnableRaisingEvents = true;
Console.WriteLine("Press any key to quit");
Console.Read();
}
}
/*
Press any key to quit

View File

@@ -0,0 +1,26 @@
/*
* C# Program to Use StreamReader to Read Entire Line
*/
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
public sealed class Program
{
public static void Main()
{
Stream s = new FileStream(@"c:\sri\srip.txt", FileMode.Open);
using (StreamReader sr = new StreamReader(s, Encoding.UTF8))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
Console.ReadLine();
}
}
}
/*
StreamWriter writes text files. It enables easy and efficient text output. It is best placed in a using-statement to ensure it is removed from memory when no longer needed. It provides several constructors and many methods.

View File

@@ -0,0 +1,23 @@
/*
* C# Program to View the Date and time of Access of a File
*/
using System;
using System.IO;
class Program
{
static void Main()
{
FileInfo info = new FileInfo("C:\\sri\\srip.txt");
DateTime time = info.CreationTime;
Console.WriteLine("File Creation Time : {0}", time);
time = info.LastAccessTime;
Console.WriteLine("File Last Access Time : {0}", time);
time = info.LastWriteTime;
Console.WriteLine("File Last Write Time : {0} ", time);
Console.Read();
}
}
/*
File Creation Time : 8/11/2013 7:17:20 PM
File Access Time : 8/15/2013 1:08:45 PM
File Last Write Time : 8/15/2013 1:37:36 PM

View File

@@ -0,0 +1,18 @@
/*
* C# Program to View the Information of the File
*/
using System;
using System.IO;
class Program
{
static void Main()
{
FileInfo info = new FileInfo("C:\\sri\\srip.txt");
FileAttributes attributes = info.Attributes;
Console.WriteLine("Nature(Attribute) of the File : {0}",attributes);
Console.Read();
}
}
/*
Nature(Attribute) of the File : Archive

View File

@@ -0,0 +1,56 @@
using System;
using System.IO;
using System.Text;
class filexercise3
{
public static void Main()
{
string fileName = @"mytest.txt";
try
{
// Delete the file if it exists.
if (File.Exists(fileName))
{
File.Delete(fileName);
}
Console.Write("\n\n Append some text to an existing file :\n");
Console.Write("--------------------------------------------\n");
// Create the file.
using (StreamWriter fileStr = File.CreateText(fileName))
{
fileStr.WriteLine(" Hello and Welcome");
fileStr.WriteLine(" It is the first content");
fileStr.WriteLine(" of the text file mytest.txt");
}
using (StreamReader sr = File.OpenText(fileName))
{
string s = "";
Console.WriteLine(" Here is the content of the file mytest.txt : ");
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
Console.WriteLine("");
}
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"mytest.txt", true))
{
file.WriteLine(" This is the line appended at last line.");
}
using (StreamReader sr = File.OpenText(fileName))
{
string s = "";
Console.WriteLine(" Here is the content of the file after appending the text : ");
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
Console.WriteLine("");
}
}
catch (Exception MyExcep)
{
Console.WriteLine(MyExcep.ToString());
}
}
}

View File

@@ -0,0 +1,49 @@
using System;
using System.IO;
using System.Text;
class filexercise3
{
public static void Main()
{
string fileName = @"mytest.txt";
int count;
try
{
// Delete the file if it exists.
if (File.Exists(fileName))
{
File.Delete(fileName);
}
Console.Write("\n\n Count the number of lines in a file :\n");
Console.Write("------------------------------------------\n");
// Create the file.
using (StreamWriter fileStr = File.CreateText(fileName))
{
fileStr.WriteLine(" test line 1");
fileStr.WriteLine(" test line 2");
fileStr.WriteLine(" Test line 3");
fileStr.WriteLine(" test line 4");
fileStr.WriteLine(" test line 5");
fileStr.WriteLine(" Test line 6");
}
using (StreamReader sr = File.OpenText(fileName))
{
string s = "";
count=0;
Console.WriteLine(" Here is the content of the file mytest.txt : ");
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
count++;
}
Console.WriteLine("");
}
Console.Write(" The number of lines in the file {0} is : {1} \n\n",fileName,count);
}
catch (Exception MyExcep)
{
Console.WriteLine(MyExcep.ToString());
}
}
}

View File

@@ -0,0 +1,57 @@
using System;
using System.IO;
using System.Text;
public class SimpleFileMove
{
static void Main()
{
string sfileName = @"mytest.txt";
string tfileName = @"mynewtest.txt";
/* string sourcefolder = "path"; // you can mention the path of source folder
string targetfolder = "path"; // you can mention the path of target folder
string sourceFile = System.IO.Path.Combine(sourcefolder, sfileName); // combine the source file with path
string targetFile = System.IO.Path.Combine(targetfolder, tfileName); // combine the target file with path */
if (File.Exists(sfileName))
{
File.Delete(sfileName);
}
if (File.Exists(tfileName))
{
File.Delete(tfileName);
}
Console.Write("\n\n Create a file and move the file in same folder to another name :\n");
Console.Write("----------------------------------------------------------------------\n");
// Create the file.
using (StreamWriter fileStr = File.CreateText(sfileName))
{
fileStr.WriteLine(" Hello and Welcome");
fileStr.WriteLine(" It is the first content");
fileStr.WriteLine(" of the text file mytest.txt");
}
using (StreamReader sr = File.OpenText(sfileName))
{
string s = "";
Console.WriteLine(" Here is the content of the file {0} : ",sfileName);
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
Console.WriteLine("");
}
System.IO.File.Move(sfileName, tfileName); // move a file to another name in same location:
Console.WriteLine(" The file {0} successfully moved to the name {1} in the same directory.",sfileName,tfileName );
using (StreamReader sr = File.OpenText(tfileName))
{
string s = "";
Console.WriteLine(" Here is the content of the file {0} : ",tfileName);
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
Console.WriteLine("");
}
Console.ReadKey();
}
}

View File

@@ -0,0 +1,40 @@
using System;
using System.IO;
class WriteTextFile
{
static void Main()
{
string fileName = @"mytest.txt";
string[] ArrLines ;
int n,i;
Console.Write("\n\n Create a file and write an array of strings :\n");
Console.Write("---------------------------------------------------\n");
if (File.Exists(fileName))
{
File.Delete(fileName);
}
Console.Write(" Input number of lines to write in the file :");
n= Convert.ToInt32(Console.ReadLine());
ArrLines=new string[n];
Console.Write(" Input {0} strings below :\n",n);
for(i=0; i<n; i++)
{
Console.Write(" Input line {0} : ",i+1);
ArrLines[i] = Console.ReadLine();
}
System.IO.File.WriteAllLines(fileName, ArrLines);
using (StreamReader sr = File.OpenText(fileName))
{
string s = "";
Console.Write("\n The content of the file is :\n",n);
Console.Write("----------------------------------\n");
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(" {0} ",s);
}
Console.WriteLine();
}
}
}

View File

@@ -0,0 +1,60 @@
using System;
using System.IO;
using System.Text;
public class SimpleFileCopy
{
static void Main()
{
string sfileName = @"mytest.txt";
string tfileName = @"mynewtest.txt";
// Delete the file if it exists.
if (File.Exists(sfileName))
{
File.Delete(sfileName);
}
Console.Write("\n\n Create a file and copy the file :\n");
Console.Write("---------------------------------------\n");
// Create the file.
using (StreamWriter fileStr = File.CreateText(sfileName))
{
fileStr.WriteLine(" Hello and Welcome");
fileStr.WriteLine(" It is the first content");
fileStr.WriteLine(" of the text file mytest.txt");
}
using (StreamReader sr = File.OpenText(sfileName))
{
string s = "";
Console.WriteLine(" Here is the content of the file {0} : ",sfileName);
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
Console.WriteLine("");
}
/* string sourcefolder = "path"; // you can mention the path of source folder
string targetfolder = "path"; // you can mention the path of target folder
string sourceFile = System.IO.Path.Combine(sourcefolder, sfileName); // combine the source file with path
string targetFile = System.IO.Path.Combine(targetfolder, tfileName); // combine the target file with path */
/* Create a new target folder if not exists
if (!System.IO.Directory.Exists(targetfolder))
{
System.IO.Directory.CreateDirectory(targetfolder);
}
System.IO.File.Copy(sourceFile, destFile, true); // overwrite the target file if it already exists. */
System.IO.File.Copy(sfileName, tfileName, true);
Console.WriteLine(" The file {0} successfully copied to the name {1} in the same directory.",sfileName,tfileName );
using (StreamReader sr = File.OpenText(tfileName))
{
string s = "";
Console.WriteLine(" Here is the content of the file {0} : ",tfileName);
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
Console.WriteLine("");
}
Console.ReadKey();
}
}

View File

@@ -0,0 +1,50 @@
using System;
using System.IO;
class filexercise14
{
static void Main()
{
string fileName = @"mytest.txt";
string[] ArrLines ;
int n,i,l,m=1;
Console.Write("\n\n Read last n number of lines from a file :\n");
Console.Write("-----------------------------------------------\n");
if (File.Exists(fileName))
{
File.Delete(fileName);
}
Console.Write(" Input number of lines to write in the file :");
n= Convert.ToInt32(Console.ReadLine());
ArrLines=new string[n];
Console.Write(" Input {0} strings below :\n",n);
for(i=0; i<n; i++)
{
Console.Write(" Input line {0} : ",i+1);
ArrLines[i] = Console.ReadLine();
}
System.IO.File.WriteAllLines(fileName, ArrLines);
Console.Write("\n Input last how many numbers of lines you want to display :");
l = Convert.ToInt32(Console.ReadLine());
m=l;
if(l>=1 && l<=n)
{
Console.Write("\n The content of the last {0} lines of the file {1} is : \n",l,fileName);
if (File.Exists(fileName))
{
for(i=n-l; i<n; i++)
{
string[] lines = File.ReadAllLines(fileName);
Console.Write(" The last no {0} line is : {1} \n",m,lines[i]);
m--;
}
}
}
else
{
Console.WriteLine(" Please input the correct line no.");
}
Console.WriteLine();
}
}

View File

@@ -0,0 +1,36 @@
using System;
using System.IO;
class filexercise12
{
static void Main()
{
string fileName = @"mytest.txt";
string[] ArrLines ;
int n,i;
Console.Write("\n\n Create and read the last line of a file :\n");
Console.Write("-----------------------------------------------\n");
if (File.Exists(fileName))
{
File.Delete(fileName);
}
Console.Write(" Input number of lines to write in the file :");
n= Convert.ToInt32(Console.ReadLine());
ArrLines=new string[n];
Console.Write(" Input {0} strings below :\n",n);
for(i=0; i<n; i++)
{
Console.Write(" Input line {0} : ",i+1);
ArrLines[i] = Console.ReadLine();
}
System.IO.File.WriteAllLines(fileName, ArrLines);
Console.Write("\n The content of the last line of the file {0} is :\n",fileName);
if (File.Exists(fileName))
{
string[] lines = File.ReadAllLines(fileName);
Console.WriteLine(" {0}",lines[n-1]);
}
Console.WriteLine();
}
}

View File

@@ -0,0 +1,54 @@
using System;
using System.IO;
class WriteTextFile
{
static void Main()
{
string fileName = @"mytest.txt";
string[] ArrLines;
string str;
int n,i;
Console.Write("\n\n Create and write some line of text which does not contain a given string in a line :\n");
Console.Write("------------------------------------------------------------------------------------------\n");
if (File.Exists(fileName))
{
File.Delete(fileName);
}
Console.Write(" Input the string to ignore the line : ");
str = Console.ReadLine();
Console.Write(" Input number of lines to write in the file : ");
n= Convert.ToInt32(Console.ReadLine());
ArrLines=new string[n];
Console.Write(" Input {0} strings below :\n",n);
for(i=0; i<n; i++)
{
Console.Write(" Input line {0} : ",i+1);
ArrLines[i] = Console.ReadLine();
}
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(@"mytest.txt"))
{
foreach (string line in ArrLines)
{
if (!line.Contains(str)) // write the line to the file If it doesn't contain the string in str
{
file.WriteLine(line);
}
}
}
using (StreamReader sr = File.OpenText(fileName))
{
string s = "";
Console.Write("\n The line has ignored which contain the string '{0}'. \n",str);
Console.Write("\n The content of the file is :\n",n);
Console.Write("----------------------------------\n");
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(" {0} ",s);
}
Console.WriteLine();
}
}
}

View File

@@ -0,0 +1,45 @@
using System;
using System.IO;
class filexercise13
{
static void Main()
{
string fileName = @"mytest.txt";
string[] ArrLines ;
int n,i,l;
Console.Write("\n\n Read a specific line from a file :\n");
Console.Write("----------------------------------------\n");
if (File.Exists(fileName))
{
File.Delete(fileName);
}
Console.Write(" Input number of lines to write in the file :");
n= Convert.ToInt32(Console.ReadLine());
ArrLines=new string[n];
Console.Write(" Input {0} strings below :\n",n);
for(i=0; i<n; i++)
{
Console.Write(" Input line {0} : ",i+1);
ArrLines[i] = Console.ReadLine();
}
System.IO.File.WriteAllLines(fileName, ArrLines);
Console.Write("\n Input which line you want to display :");
l = Convert.ToInt32(Console.ReadLine());
if(l>=1 && l<=n)
{
Console.Write("\n The content of the line {0} of the file {1} is : \n",l,fileName);
if (File.Exists(fileName))
{
string[] lines = File.ReadAllLines(fileName);
Console.WriteLine(" {0}",lines[l-1]);
}
}
else
{
Console.WriteLine(" Please input the correct line no.");
}
Console.WriteLine();
}
}

View File

@@ -0,0 +1,49 @@
using System;
using System.IO;
using System.Text;
class filexercise11
{
public static void Main()
{
string fileName = @"mytest.txt";
try
{
// Delete the file if it exists.
if (File.Exists(fileName))
{
File.Delete(fileName);
}
Console.Write("\n\n Read the first line from a file :\n");
Console.Write("---------------------------------------\n");
// Create the file.
using (StreamWriter fileStr = File.CreateText(fileName))
{
fileStr.WriteLine(" test line 1");
fileStr.WriteLine(" test line 2");
fileStr.WriteLine(" Test line 3");
}
using (StreamReader sr = File.OpenText(fileName))
{
string s = "";
Console.WriteLine(" Here is the content of the file mytest.txt : ");
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
Console.WriteLine("");
}
Console.Write("\n The content of the first line of the file is :\n");
if (File.Exists(fileName))
{
string[] lines = File.ReadAllLines(fileName);
Console.Write(lines[0]);
}
Console.WriteLine();
}
catch (Exception MyExcep)
{
Console.WriteLine(MyExcep.ToString());
}
}
}