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
301 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
1 Comment
 
LVL 74

Accepted Solution

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Pass through dll 2 36
C# Application Local DB Connection String 23 58
Data Saving(2) 2 21
Copy/Clone an object. 9 13
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…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now