// Wipe file
public static void Wipe(string fileName)
{
if (File.Exists(fileName))
{
File.SetAttributes(fileName, FileAttributes.Normal);
Random rnd = new Random((int)DateTime.Now.Ticks);
FileStream file = File.Open(fileName, FileMode.Open, FileAccess.Write);
int fileLength = (int)file.Length;
int offset = 0;
const int BUFFER_SIZE = 100;
byte[][] buffers = new byte[4][];
buffers[0] = new byte[BUFFER_SIZE];
buffers[1] = new byte[BUFFER_SIZE];
buffers[2] = new byte[BUFFER_SIZE];
buffers[3] = new byte[BUFFER_SIZE];
rnd.NextBytes(buffers[0]);
rnd.NextBytes(buffers[2]);
for (int i = 0; i < BUFFER_SIZE; i++)
{
buffers[1][i] = 0;
buffers[3][i] = 0xff;
}
while (offset < (fileLength - BUFFER_SIZE))
{
for (int i = 0; i < buffers.Length; i++)
{
file.Seek(offset, SeekOrigin.Begin);
file.Write(buffers[0], 0, BUFFER_SIZE);
file.Flush();
}
offset += 100;
}
for (int i = 0; i < buffers.Length; i++)
{
file.Seek(offset, SeekOrigin.Begin);
file.Write(buffers[0], 0, fileLength - offset);
file.Flush();
}
file.Close();
file = File.Open(fileName, FileMode.Truncate, FileAccess.Write);
file.Flush();
file.Close();
File.Delete(fileName);
}
}
Saturday, May 30, 2009
File lock
// Lock files so that no one else can access it:
// --------
// Method 1
// --------
string filename = "c:\\sample.htm";
FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read,
FileShare.None); //locks file
// ...
stream.Close(); //unlocks file
// --------
// Method 1
// --------
string filename = "c:\\sample.htm";
FileStream stream = File.Open(filename, FileMode.Open);
stream.Lock(0, stream.Length); //locks file
// ...
stream.Unlock(0, stream.Length); //unlocks file
// --------
// Method 1
// --------
string filename = "c:\\sample.htm";
FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read,
FileShare.None); //locks file
// ...
stream.Close(); //unlocks file
// --------
// Method 1
// --------
string filename = "c:\\sample.htm";
FileStream stream = File.Open(filename, FileMode.Open);
stream.Lock(0, stream.Length); //locks file
// ...
stream.Unlock(0, stream.Length); //unlocks file
List Files in a Directory in C#
// List Files in a Directory in C#
// You can get an array of files (their path) by using:
fileArray = Directory.GetFiles(yourPath);
// You can display all of the images in a directory by using:
ArrayList pics = new ArrayList();
string myFile;
string code;
const string YOUR_DIRECTORY = "/images/";
foreach (myFile in Directory.GetFiles(Server.MapPath(YOUR_DIRECTORY), "*.gif"))
{
image = YOUR_DIRECTORY + Path.GetFileName(myFile);
code = <"img src=\"" + image + "\">";
// add the image to your ArrayList
pics.Add(code);
}
// Bind the pics to a DataList
dlImages.DataSource = pics;
dlImages.DataBind;
// You can get an array of files (their path) by using:
fileArray = Directory.GetFiles(yourPath);
// You can display all of the images in a directory by using:
ArrayList pics = new ArrayList();
string myFile;
string code;
const string YOUR_DIRECTORY = "/images/";
foreach (myFile in Directory.GetFiles(Server.MapPath(YOUR_DIRECTORY), "*.gif"))
{
image = YOUR_DIRECTORY + Path.GetFileName(myFile);
code = <"img src=\"" + image + "\">";
// add the image to your ArrayList
pics.Add(code);
}
// Bind the pics to a DataList
dlImages.DataSource = pics;
dlImages.DataBind;
Set a file as read-only
public void SetReadOnly(string fullName, bool readOnly)
{
FileInfo filePath = new FileInfo(fullName);
try
{
if (filePath.Exists)
{
FileAttributes attr;
if (readOnly)
{
attr = (FileAttributes)
(filePath.Attributes | FileAttributes.ReadOnly);
}
else
{
attr = (FileAttributes)
(filePath.Attributes - FileAttributes.ReadOnly);
}
File.SetAttributes(filePath.FullName, attr);
}
}
catch (IOException e)
{
System.Diagnostics.Debug.WriteLine(e.Source);
System.Diagnostics.Debug.WriteLine(e.Message);
System.Diagnostics.Debug.WriteLine(e.StackTrace);
}
}
{
FileInfo filePath = new FileInfo(fullName);
try
{
if (filePath.Exists)
{
FileAttributes attr;
if (readOnly)
{
attr = (FileAttributes)
(filePath.Attributes | FileAttributes.ReadOnly);
}
else
{
attr = (FileAttributes)
(filePath.Attributes - FileAttributes.ReadOnly);
}
File.SetAttributes(filePath.FullName, attr);
}
}
catch (IOException e)
{
System.Diagnostics.Debug.WriteLine(e.Source);
System.Diagnostics.Debug.WriteLine(e.Message);
System.Diagnostics.Debug.WriteLine(e.StackTrace);
}
}
Write a binary file
public bool WriteBinaryFile(string filePath, byte[] contents)
{
bool okok = true;
try
{
using (FileStream fs = new FileStream(filePath, FileMode.CreateNew))
{
using (BinaryWriter w = new BinaryWriter(fs))
{
w.Write(contents);
}
}
}
catch (IOException e)
{
okok = false;
}
return okok;
}
{
bool okok = true;
try
{
using (FileStream fs = new FileStream(filePath, FileMode.CreateNew))
{
using (BinaryWriter w = new BinaryWriter(fs))
{
w.Write(contents);
}
}
}
catch (IOException e)
{
okok = false;
}
return okok;
}
Recursively Search Directory for a File
System.Collections.ArrayList files = new System.Collections.ArrayList();
DirSearch(@"C:",ref files);
//Method to Search Directory for specified file Type.Paste it outside the function
public void DirSearch(string sDir,ref System.Collections.ArrayList files)
{
try
{
foreach (string d in System.IO.Directory.GetDirectories(sDir))
{
foreach (string f in System.IO.Directory.GetFiles(d, "*.txt"))
{
files.Add(f);
}
DirSearch(d,ref files);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
DirSearch(@"C:",ref files);
//Method to Search Directory for specified file Type.Paste it outside the function
public void DirSearch(string sDir,ref System.Collections.ArrayList files)
{
try
{
foreach (string d in System.IO.Directory.GetDirectories(sDir))
{
foreach (string f in System.IO.Directory.GetFiles(d, "*.txt"))
{
files.Add(f);
}
DirSearch(d,ref files);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
How to Read a Text File (with a single call to sReader.ReadToEnd())
public static string getFileAsString(string fileName) {
StreamReader sReader = null;
string contents = null;
try {
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
sReader = new StreamReader(fileStream);
contents = sReader.ReadToEnd();
} finally {
if(sReader != null) {
sReader.Close();
}
}
return contents;
}
StreamReader sReader = null;
string contents = null;
try {
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
sReader = new StreamReader(fileStream);
contents = sReader.ReadToEnd();
} finally {
if(sReader != null) {
sReader.Close();
}
}
return contents;
}
Subscribe to:
Posts (Atom)
