0% found this document useful (0 votes)
33 views

Practical Assignment 3: 1. Directory Program in C#

The document contains 6 programs demonstrating various file handling and database programming concepts in C#: 1. A directory listing program 2. A file copying program 3. A program to create a directory 4. A program to copy file contents to the console 5. A file copying program between locations 6. A program demonstrating serialization and deserialization of objects to XML

Uploaded by

bhagesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Practical Assignment 3: 1. Directory Program in C#

The document contains 6 programs demonstrating various file handling and database programming concepts in C#: 1. A directory listing program 2. A file copying program 3. A program to create a directory 4. A program to copy file contents to the console 5. A file copying program between locations 6. A program demonstrating serialization and deserialization of objects to XML

Uploaded by

bhagesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Practical Assignment 3

Program based on File Handling and Database Programming


Program to demonstrate use of directories, sequential access file, random access file
Program on serialization and deserialization
Program to demonstrate LINQ, based on database access using ADO.NET

1. Directory program in C#
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
using System.IO;
usingSystem.Collections;
usingSystem.Threading.Tasks;
namespaceDirectoryProgram {
class Program {
static void Main(string[] args) {
try
{
Console.WriteLine("C drive info \n\n");
String[] t = Directory.GetDirectories(@"c:\c drive");
for(inti=0;i<t.Length;i++)
{
Console.WriteLine("[" + i + "] : " + t[i].ToString());
}
Console.ReadLine();
}
catch(Exception ex)
{
}
}
}
}

2. File Handling Program


Program.cs
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
using System.IO;
namespaceFileHandlingProj {
class Program {
static void Main(string[] args) {
try
{
Console.WriteLine("Enter the file name FROM which you want to copy the contents");
String f1 = Console.ReadLine();
Console.WriteLine("Enter the file name TO which you want to copy the contents");
String f2 = Console.ReadLine();
FileStream from = new FileStream(f1, FileMode.Open, FileAccess.Read);
FileStream to = new FileStream(f2, FileMode.OpenOrCreate, FileAccess.Write);
Byte[] buff = new byte[from.Length];
from.Read(buff, 0, buff.Length);
to.Write(buff, 0, buff.Length);
from.Close();
to.Close();
Console.WriteLine("Successfully written..Now open the file!!");
}
catch(Exception ex) { Console.WriteLine("Error~~~!! " + ex.Message); }
Console.ReadLine();
} } }

3. Create directory
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
using System.IO;
usingSystem.Threading.Tasks;
namespaceCreateDirectory
{
class Program
{
static void Main(string[] args)
{
try
{
String root;
Console.WriteLine("enter PATH where you want to create directory");
String path = Console.ReadLine();
Console.WriteLine("enter directory name ");
String name = Console.ReadLine();
root = @"" + path + name;
if(!Directory.Exists(root))
{
Directory.CreateDirectory(root);
Console.WriteLine("Directory Created.Now check it!!");
}
else if(Directory.Exists(root))
{
Console.WriteLine("Directory may already exist ..please check!!");
}
}
catch(Exception ex)
{
Console.WriteLine("Error~~!! " + ex.Message);
}
Console.ReadLine();
}
}
}

4. Copy content to console


using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
using System.IO;
namespacecopyToConsole
{
class Program
{
static void Main(string[] args)
{
try
{
//FileStreamrt = new FileStream(, FileMode.Open, FileAccess.ReadWrite);
Console.WriteLine("press any key to read contents of the file _ _ _");
Console.ReadKey();
Console.WriteLine("Contents of the file are : \n --------------------------------------------------\n \n \n");
string text = System.IO.File.ReadAllText(@"c:\110\file1.txt");
Console.WriteLine(text);
Console.ReadLine();
}
catch (Exception ex)
{
}
}
}
}

5. File copy from one location to another location


using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
using System.IO;
usingSystem.Threading.Tasks;
namespacefileCopy
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("enter file's location and name ");
string f1 = Console.ReadLine();
Console.WriteLine("enter location to paste");
stringloc = Console.ReadLine();
FileInfo t = new FileInfo(f1);
t.CopyTo(loc+""+t.Name );
Console.WriteLine("successfully pasted-----------");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}

6. Program on serialization and deserialization


Program.cs
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
using System.IO;
usingSystem.Xml;
usingSystem.Xml.Serialization;
namespaceSerializationAndDeserialization
{
class Program
{
static void Main(string[] args)
{
class1 t1=new class1();
t1.make = "make 1";
t1.stno = "stno1";
t1.model = "model1";
class1 t2 = new class1();
t2.make = "make 2";
t2.stno = "stno2";
t2.model = "model2";
List<class1> class1 = new List<class1>() { t1, t2 };
XmlSerializerser = new XmlSerializer(typeof(List<class1>));
TextWritertxtw = new StreamWriter(@"c:\110\outpu.xml");
ser.Serialize(txtw, class1);
txtw.Close();
XmlSerializerdeser = new XmlSerializer(typeof(List<class1>));
TextReadertxtr = new StreamReader(@"c:\110\outpu.xml");
List<class1>tt;
tt = (List<class1>)deser.Deserialize(txtr);
for (inti = 0; i<tt.Count;i++ )
{
Console.WriteLine();
Console.WriteLine(tt[i].make);
Console.WriteLine(tt[i].model);
Console.WriteLine(tt[i].stno);
}
txtr.Close();
Console.ReadLine();

}
}
}
namespaceSerializationAndDeserialization
{
[Serializable()]
public class class1
{
[XmlElement("stockNo")]
public string stno { get; set; }
[XmlElement("Make")]
public string make { get; set; }
[XmlElement("Model")]
public string model{ get; set; }
}
}

You might also like