CreateDataFile()

By Sockreg on Dec 05, 2009

Well, the .NET framework already contains the functionality provided by this snippet, however this snippet shows how to use some IO namespace classes and functionality.

/*
Author: Sockreg
Occupation: Student, .NET Enthusiast
Functionality: Creates a file in an existing directory (the function will create the directory
               if it is not found on your pc). The extension for the file is selected through the
               enumeration provided by the IOMisc class. Text can be written to the file on creation.
*/

using System;
using System.Text;
using System.IO;

class Program {

    static void Main(string[] args) {
        try {
            //create an instance of the IOMisc class
            IOMisc objectInstance = new IOMisc();

            /*
             Parameters: file name (without extension)
                         directory
                         extension
                         input text for file
             */

            objectInstance.CreateDataFile("TestFile", @"C:\MyDirectory\MyData", 
                                IOMisc.DataFile.txt, 
                                String.Format("Created: {0} {3}OS:{1} {3}Number of cores: {2}", 
                                DateTime.Now.ToLongDateString(), Environment.OSVersion,
                                Environment.ProcessorCount, Environment.NewLine));
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        }
    }
}

class IOMisc {

    //enum containing file types 
    public enum DataFile { txt, dat, ini, csv };

    public void CreateDataFile(String fileName, String directory, 
                               DataFile extension, String text) {
        try {
        var fileNameWithExtension = String.Format("{0}.{1}", 
                Path.GetFileNameWithoutExtension(fileName),
                                            extension.ToString());
        var directoryExists = Directory.Exists(directory) ? 
                            null : Directory.CreateDirectory(directory);
        if (Directory.Exists(directory)) {
            var fullPath = Path.Combine(directory, fileNameWithExtension);
            DirectoryInfo myDirectory = new DirectoryInfo(directory);
            bool fileFound = false;
            foreach (FileInfo myFile in myDirectory.GetFiles()) {
               if (fileNameWithExtension.ToUpper() 
                                    == myFile.FullName.ToUpper()) 
               { fileFound = true; }
            }
                if (fileFound == false) {
                    FileInfo fileToCreate = new FileInfo(fullPath);
                    FileStream dataFileStream = fileToCreate.Open(FileMode.CreateNew,
                                                            FileAccess.ReadWrite);
                    var byteArray = Encoding.Default.GetBytes(text);
                    dataFileStream.Write(byteArray, 0, byteArray.Length);
                    dataFileStream.Flush(); dataFileStream.Close();
                }
                else {
                    throw new ApplicationException("File already exists");
                }
            }
        }
    catch (Exception ex) {
        throw ex;
    }
  }
}

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.