Solved

How to copy an existing file to newly created file, from a C# console application written with VS2010?

Posted on 2014-03-15
1
316 Views
Last Modified: 2014-03-31
I am developing a C# console application with VS2010.  I would like to copy an input file  to a newly created file "bankFilePath" within the following method:

string bankFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, MyGlobals.BASE_FILE_Name + ".ard.out");

Open in new window

------------------------------------------------------------------

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;

namespace ReadBinary
{
    public static class MyGlobals
    {
        //the offset value of the current check is the sum of the offset and length of the PRIOR check and thus
        //the reason it is a global variable is to store the value of the prior check (it's offset + length)
        public static int intcheckOffset = 0;

        //use of a constant so that if the directory path changes, only 1 change is needed and it's here 
        public static string BASE_DIR = @"L:\FINSYS\FSMain\C\TEST\";

        public static string BASE_FILE_Name;      
    }

    class BinaryFileTest
    {
        public static string GetFileTitleOnly(string fileName)
        {
            string fileTitle = string.Empty;

            if (fileName.Contains('.'))
            {
                int startIndex = 0;
                int dotIndex = int.MinValue;

                if (fileName.Contains('\\'))
                {
                    startIndex = fileName.LastIndexOf('\\');
                }

                dotIndex = fileName.IndexOf('.');

                fileTitle = fileName.Substring(startIndex, dotIndex);
            }

            return fileTitle;
        }

        static void Main()
        {
            //store any errors during field validations to this error file
            var watch = Stopwatch.StartNew();

            MyGlobals.BASE_FILE_Name = "CZ10." + DateTime.Now.ToString("yyyyMMdd.HHmmss");

            File.Open(MyGlobals.BASE_DIR + MyGlobals.BASE_FILE_Name + ".ard", FileMode.Create).Close();

            System.Reflection.Assembly currentRunningAssembly = System.Reflection.Assembly.GetExecutingAssembly();

            string errorFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, "error.txt");
            string indFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, MyGlobals.BASE_FILE_Name + ".ard.ind");
            string bankFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, MyGlobals.BASE_FILE_Name + ".ard.out");

            //delete the error file if it presently exists so that a new error file can be created in the event any errors are discovered
            if (File.Exists(errorFilePath))
            {
                File.Delete(errorFilePath);
            }

            //delete the bank output file if it presently exists so that a new bank output file can be created in the event any errors are discovered
            if (File.Exists(indFilePath))
            {
                File.Delete(indFilePath);
            }

            //delete the bank check image file if it presently exists so that a new bank check image file can be created in the event any errors are discovered
            if (File.Exists(bankFilePath))
            {
                File.Delete(bankFilePath);
            }
            
            //the incoming check image file does not have a file extension
            DirectoryInfo parentDirectory = new DirectoryInfo(MyGlobals.BASE_DIR);

            foreach (FileInfo file in parentDirectory.GetFiles())
            {
                if (!string.Equals(file.Name, MyGlobals.BASE_FILE_Name + ".ard", StringComparison.InvariantCultureIgnoreCase) &&
                    !string.Equals(GetFileTitleOnly(file.Name), GetFileTitleOnly(currentRunningAssembly.ManifestModule.Name), StringComparison.InvariantCultureIgnoreCase))
                {
                    StreamWriter sw1 = new StreamWriter(errorFilePath);
                    ProcessFile(file.FullName, sw1);                    
                }
            }

Open in new window

0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39932211
Use the Copy method of the File class.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question