Csharp/C Sharp/File Stream/File Info — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:45, 26 мая 2010
Содержание
- 1 Copying A File
- 2 Create DriveInfo object from FullName of FileInfo
- 3 Create FileStream from FileInfo
- 4 Create Stream from FileInfo from OpenText method
- 5 Creating Files
- 6 Deleting Files
- 7 Get Directory information from FileInfo
- 8 Get File Attributes from FileInfo
- 9 Get file Creation time from FileInfo
- 10 Get Last updated, accessed and write time
- 11 Moving A Files
- 12 Open Existing File
Copying A File
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
using System.IO;
namespace Client.Chapter_11___File_and_Streams
{
public class CopyingAFile {
static void Main(string[] args)
{
FileInfo MyFile = new FileInfo(@"c:\Projects\Testing.txt");
MyFile.Create();
MyFile.CopyTo(@"c:\Projects\MyFolder\Moved Testing.txt");
//or
MyFile.CopyTo(@"c:\Projects\MyFolder\Moved Testing.txt", true);
}
}
}
Create DriveInfo object from FullName of FileInfo
using System;
using System.IO;
class MainClass {
static void Main(string[] args) {
FileInfo file = new FileInfo("c:\\a.txt");
// Display drive information.
DriveInfo drv = new DriveInfo(file.FullName);
Console.Write("Drive: ");
Console.WriteLine(drv.Name);
}
}
Create FileStream from FileInfo
using System;
using System.IO;
class FileFileInfoApp
{
static void Main(string[] args)
{
FileInfo f = new FileInfo("Bar.txt");
FileStream fs = f.Create();
StreamWriter w = new StreamWriter(fs);
w.Write("Hello World");
w.Close();
fs = f.Open(FileMode.Open, FileAccess.Read, FileShare.None);
StreamReader r = new StreamReader(fs);
string t;
while ((t = r.ReadLine()) != null){
Console.WriteLine(t);
}
w.Close();
fs.Close();
f.Delete();
}
}
Create Stream from FileInfo from OpenText method
using System;
using System.IO;
public class LicenseViewer {
public static void Main() {
FileInfo license = new FileInfo("c:\\LICENSE.txt");
StreamReader txtIn = license.OpenText();
String line;
do {
line = txtIn.ReadLine();
Console.WriteLine(line);
} while (line != null);
}
}
Creating Files
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
using System.IO;
namespace Client.Chapter_11___File_and_Streams
{
public class CreatingFiles {
public CreatingFiles()
{
FileInfo MyFile = new FileInfo(@"c:\Projects\Testing.txt");
MyFile.Create();
}
}
}
Deleting Files
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
using System.IO;
namespace Client.Chapter_11___File_and_Streams
{
public class DeletingFiles {
static void Main(string[] args)
{
FileInfo MyFile = new FileInfo(@"c:\Projects\Testing.txt");
MyFile.Create();
MyFile.Delete();
}
}
}
Get Directory information from FileInfo
using System;
using System.IO;
class MainClass {
static void Main(string[] args) {
FileInfo file = new FileInfo("c:\\a.txt");
// Display directory information.
DirectoryInfo dir = file.Directory;
Console.WriteLine("Checking directory: " + dir.Name);
Console.WriteLine("In directory: " + dir.Parent.Name);
Console.Write("Directory exists: ");
Console.WriteLine(dir.Exists.ToString());
}
}
Get File Attributes from FileInfo
using System;
using System.IO;
class Class1 {
static void Main(string[] args) {
string[] cla = Environment.GetCommandLineArgs();
if (cla.GetUpperBound(0) == 1) {
FileInfo fi = new FileInfo(cla[1]);
fi.Delete();
Console.WriteLine("File : " + cla[1]);
Console.WriteLine("Attributes: " + fi.Attributes.ToString());
Console.WriteLine("File Deleted...");
} else
Console.WriteLine("Usage: rm <filename>");
}
}
Get file Creation time from FileInfo
using System;
using System.IO;
class Class1 {
static void Main(string[] args) {
string[] cla = Environment.GetCommandLineArgs();
if (cla.GetUpperBound(0) == 2) {
FileInfo fi = new FileInfo(cla[1]);
fi.MoveTo(cla[2]);
Console.WriteLine("File Created : " + fi.CreationTime.ToString());
Console.WriteLine("Moved to : " + cla[2]);
} else
Console.WriteLine("Usage: mv <source file> <destination file>");
}
}
Get Last updated, accessed and write time
using System;
using System.IO;
class MainClass {
static void Main(string[] args) {
FileInfo file = new FileInfo("c:\\a.txt");
Console.WriteLine("Checking file: " + file.Name);
Console.WriteLine("File exists: " + file.Exists.ToString());
if (file.Exists) {
Console.Write("File created: ");
Console.WriteLine(file.CreationTime.ToString());
Console.Write("File last updated: ");
Console.WriteLine(file.LastWriteTime.ToString());
Console.Write("File last accessed: ");
Console.WriteLine(file.LastAccessTime.ToString());
Console.Write("File size (bytes): ");
Console.WriteLine(file.Length.ToString());
Console.Write("File attribute list: ");
Console.WriteLine(file.Attributes.ToString());
}
}
}
Moving A Files
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
using System.IO;
namespace Client.Chapter_11___File_and_Streams
{
public class MovingAFiles {
static void Main(string[] args)
{
FileInfo MyFile = new FileInfo(@"c:\Projects\Testing.txt");
MyFile.Create();
MyFile.MoveTo(@"c:\Projects\MyFolder\Moved Testing.txt");
MyFile.MoveTo(@"C:|projects\MyFolder");
}
}
}
Open Existing File
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
using System.IO;
namespace Client.Chapter_11___File_and_Streams
{
public class OpenExistingFile {
static void Main(string[] args)
{
FileInfo MyFile = new FileInfo(@"c:\Projects\Testing.txt");
FileStream MyStream;
MyStream = MyFile.Open(FileMode.Open, FileAccess.Read, FileShare.None);
}
}
}