Nilanjan Saha
Nilanjan Saha

Reputation: 135

'System.IO.FileInfo' does not contain a definition for 'GetFiles'

In my selenium webdriver code,I am using C# for coding purpose. As per requirement,i need to check in Reports folder if there any existing reports, then i need to clear all the reports before starting new run. And in am using Visual studio Express 2012 for web.

Below is the code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.FileInfo;
using System.Collections;



namespace GoalPlanAutomation.Reports
{
    class ReportUtil
    {
        public static int scriptNumber = 1;
        public static String indexResultFilename;
        public static String currentDir;
        public static String currentSuiteName;
        public static int tcid;
        // public static String currentSuitePath;

        public static double passNumber;
        public static double failNumber;
        public static bool newTest = true;
        public static ArrayList description = new ArrayList();
        public static ArrayList keyword = new ArrayList();
        public static ArrayList teststatus = new ArrayList();
        public static ArrayList screenShotPath = new ArrayList();

        public static void startTesting(String filename, String testStartTime,
        String env, String rel, String browser, String testSiteUrl) {
        indexResultFilename = filename;
        // currentDir=indexResultFilename.Substring
        currentDir = indexResultFilename.Substring(0,
        indexResultFilename.LastIndexOf("\\"));

        // Clear Test Report folder
        FileInfo dir = new FileInfo(currentDir);
        FileInfo[] allFiles =FileInfo.GetFiles();

        //FileInfo[] allFiles = dir.listFiles();
        for (int i = 0; i < allFiles.Length; i++) {
            allFiles[i].Delete();
        }
    }
}

Here FileInfo.GetFiles() showing up error and error says,

'System.IO.FileInfo' does not contain a definition for 'GetFiles'.

Please let me know what's the problem with the code.

Upvotes: 3

Views: 3306

Answers (2)

Delphi.Boy
Delphi.Boy

Reputation: 1216

You should use a DirectoryInfo instead of FileInfo. And for getting currentDir you would better use Path.GetDirectoryName() instead of Substring.

Upvotes: 1

crthompson
crthompson

Reputation: 15875

This is because FileInfo, does not have a method called GetFiles.

You may be confusing it with DirectoryInfo, which does.

You would want something like this:

FileInfo[] allFiles = DirectoryInfo.GetFiles(currentDir);

for (int i = 0; i < allFiles.Length; i++) {
    allFiles[i].Delete();

Upvotes: 1

Related Questions