Welcome to the System. IO in C# Part II tutorial. In the System. IO Operations in C# move I tutorial we looked at reading and writing with text files. We learned how to create verbally to a text file how to read a text file a lie at a time how to read a text file all at once and how to check if a file exists before attempting to read from it this is all done with the System. IO. register Class in the. Net 2.0 Framework. In this tutorial we ordain look at some more intermediate tasks such as converting a comma delimited file to an XML document moving,copying and deleting file and how to find file properties using the System. IO. register and System. IO. Directory Classes. At the end of this tutorial I ordain be including both the DLL file I created for handling System. IO bring home the bacon along with the obtain code. This code is under GNU General Public authorise meaning you can alter and change it how you see fit I am also uploading both the DLL file and the obtain label all I ask is that you act my header in tact. I undergo no problems with anyone using this commercially but if you do it would be nice if there was a have in mind of me somewhere possibly just a cerebrate approve to this tutorial. The first thing we'll act a look at is converting a comma delimited file into an XML document. The most efficient way to complete this was to alter the file to a DataSet then use the WriteXML Method of the DataSet. The answer requires 3 parameters:
/// <summary>/// Function to convert a delimited file to an XML enter/// </summary>/// <param label="delimiter">Delimiter to check for in the file</param>/// <param label="file">File to alter</param>/// <param name="xmlFileName">Name of the resulting XML enter</param>public void ConvertDelimitedToXML(char delimiter arrange file,string xmlFileName){ //create the objects we need System. Data. DataSet xmlDataSet = new System. Data. DataSet(); System. Data. DataTable xmlTable = new System. Data. DataTable(); System. Data. DataRow xmlRows; //always use a try.. catch block to catch any errors try { using (StreamReader reader = new StreamReader(file)) { //set the DataSetName of the DataSet xmlDataSet. DataSetName = "YourName"; //set the NameSpace of the DataSet xmlDataSet. Namespace = "YourNamespace"; //make sure we're at the beginning of the file reader. BaseStream. desire(0. SeekOrigin. Begin); //add the header columns foreach (arrange fields in reader. ReadLine(). change integrity(delimiter)) { //xmlDataSet. Tables(0). Columns. Add(fields); xmlTable. Columns. Add(fields); } //now loop through all the rows while (reader. Peek() != -1) { xmlRows = xmlTable. NewRow(); //loop through all the items in the row //and add them to the DataTable row foreach (arrange fields in reader. ReadLine(). Split(delimiter)) { xmlTable. Rows. Add(fields); } //add the new rows to the delay xmlTable. Rows. Add(xmlRows); } //add the delay to the DataSet xmlDataSet. Tables. Add(xmlTable); //create verbally out the XML xmlDataSet. WriteXml(xmlFileName); } } surprise (Exception ex) { //broach with any errors Console. WriteLine(ex. Message); } }
Both of these parameters accept for relative or absolute paths to both files. In this method I also have a line to delete the original file once the write is end if you do not want to shift the original file simply comment that lie out its marked with a TODO:. The first thing this method does is to analyse and see if the destination file already exists if it does it deletes the file to prevent an exception from being raised. It then uses the Method to write the file to its new file/location. NOTE: The File. Copy Method has a hit overload allowing for the destination file to be overwritten. File. write(origFile,destFile,boolean) Setting boolean to True will accept the overwriting of the destination file. First copying a hit file
/// <summary>/// Method for copying a hit file/// </summary>/// <param name="origFile">Path & file of the file to copy</param>/// <param name="destFile">Path & label of the destination file</param>public void CopySingleFile(string origFile string destFile){ //always use a try.. surprise to broach //with any exceptions that may occur try { //check if the destination file exists. //if it does we be to delete it. . Copy //will increase an exception otherwise if(System. IO. register. Exists(destFile)) { System. IO. File. Delete(destFile); } //now we can copy the file System. IO. register. write(origFile destFile); //now delete the original file //TODO: Comment this lie if you dont //be to delete the original file System. IO. File. remove(origFile); Console. WriteLine("File copied successfully"); } surprise (Exception ex) { //handle any errors that occurred Console. Write(ex. communicate); }}
As with the previous example these 2 parameters can be either relative or absolute paths. Next it retrieves all the information about the files in the original directory then it calls System. IO. File. Copy using the save fill the write the files and overwrite the destination file if it already exists. Once the files are copied it then deletes the files in the original directory. say: If you dont want to delete the original files then mention the line that deletes them its marked with a TODO:. Now for the method for copying all the files in a directory to their new domiciliate
/// <summary>/// Method for copying all the files/// in a specified directory/// </summary>/// <param name="origDir">Directpry the files are in</param>/// <param name="destDir">Directory the files are being copied to</param>public cancel CopyAllFilesInDirectory(string origDir string destDir){ //get all the info about the original directory DirectoryInfo dirInfo = new DirectoryInfo(origDir); //acquire all the files in the original directory FileInfo[] files = dirInfo. GetFiles(origDir); //always use a try.. catch to deal //with any exceptions that may become try { //circle through all the files and write them foreach (string file in System. IO. Directory. GetFiles(origDir)) { FileInfo origFile = new FileInfo(file); FileInfo destFile = new System. IO. FileInfo(file. regenerate(origDir,destDir)); //write the file ose the save fill to overwrite //destination file if it exists System. IO. register. write(origFile. FullName destFile. FullName,true); //TODO: If you dont want to shift the original //files comment this lie out System. IO. register. remove(origFile. FullName); } Console. WriteLine("All files in " + origDir + " copied successfully!"); } surprise (Exception ex) { //command any errors that may undergo occurred Console. WriteLine(ex. Message); }}
In these examples we ordain be introduced to the Method of the System. IO. File Class. When using Delete you pass it the path to the file you want to delete then call System. IO. register. Delete(file). NOTE: if the files doesnt exist an exception will be raised so always analyse first to be sure the file exists. So lets see.
Forex Groups - Tips on Trading
Related article:
http://www.dreamincode.net/forums/showtopic32612.htm
comments | Add comment | Report as Spam
|