Link to home
Start Free TrialLog in
Avatar of fwsteal
fwsteal

asked on

rename file

I'm having problems with the c# code below.
-----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace myLog_FileCopy
{
    class Program
    {
        static void Main(string[] args)
        {
            string winfile = @"C:\Log\alog.txt";
            string unixfile = @"C:\Log\Ulog.txt";
            //rename the files
            RenameWinLog(winfile);
            RenameUnixLog(unixfile);
        }

        //rename windows log file
        static void RenameWinLog(string winFile)
        {
            string winfileRenamed = @"C:\Log\aw.txt";
            if (!SourceFileExists(winFile)) return; //Error
            File.Move(winFile, winfileRenamed);
        }

        //rename unix log file
        static void RenameUnixLog(string unixFile)
        {
            string unixfileRenamed = @"C:\Log\ua.txt";
            if (!SourceFileExists(unixFile)) return; //Error
            File.Move(unixFile, unixfileRenamed);
        }


        //determine if the file exists
        bool SourceFileExists(string myFile)
        {
            if (!System.IO.File.Exists(myFile))
            {
                return false;
            }
            else
                return true;
        }
    }
}
-------------------------------------------------------------------------------------------------------------------
My error: An object reference is required for the nonstatic field, method, or property 'myLog_FileCopy.Program.SourceFileExists(string)'

How do I correct this error?
Avatar of Babycorn-Starfish
Babycorn-Starfish
Flag of United States of America image

Hi,

add static before SourceFileExist

BCS
Avatar of dstanley9
dstanley9

to be clear where to add it:

        //determine if the file exists
        static bool SourceFileExists(string myFile)
        {
            if (!System.IO.File.Exists(myFile))
            {
                return false;
            }
            else
                return true;
        }
ASKER CERTIFIED SOLUTION
Avatar of Babycorn-Starfish
Babycorn-Starfish
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial