javasocute
javasocute

Reputation: 648

If statement help ; different color to certain criteria

Im a beginner, and If statements are my weakness. I have a simple program that displays files names that are located in a certain folder. However, some files might have lines that begin with LIFT. I want to catch those files that have that certain line, and display the file name in a different color (preferably red). Here is what I have so far: Any assistance would be greatly appreciated!! Thanks!

public partial class ShippedOrders : System.Web.UI.Page
{
    class Program
    {
        static void Main()
        {
            string[] array1 = Directory.GetFiles(@"C:\Kaplan\Replies\");
            string[] array2 = Directory.GetFiles(@"C:\Kaplan\Replies\", "*.REP");
            Console.WriteLine("---Files:---");
            foreach (string name in array1)
            {
                Console.WriteLine(name);
            }
            Console.WriteLine("---REP Files: ---");
            foreach (string name in array2)
            {
                Console.WriteLine(name);
            }
        }
    }
}

Upvotes: 1

Views: 1846

Answers (3)

Miguel Angelo
Miguel Angelo

Reputation: 24182

You can change the console output color by using the Console.ForegroundColor property.

To know if the file contains the text you want, you need to open it and scan the file.

Then do this:

if (fileContainsText) Console.ForegroundColor = ConsoleColor.Red;
else Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(name);

EDIT

I didn't notice you were trying to write to Console inside an ASP.NET server page... in that case you need to tell us what kind of app you are creating... is it a Console application, a WebApplication or a Website... it depends.

The use of Console is not suited for web applications.

EDIT 2

By the way, you may use the Console only in console applications. A console application is a stand-alone windows application, that is different from a web application.

If you ever want to create a console app, in the New Project window, you can find it under Windows category, then you can find a project type called Console Application.

Upvotes: 1

Nick Rolando
Nick Rolando

Reputation: 26157

Directory.GetFiles(directoryPath) will return an array of strings listing the file names (full paths) within that directory. You're going to have to actually open and read each file, using the string array returned. Read each file line by line in a loop and test if any lines begins with "LIFT".

Also the way you set up your code-behind for this webpage is funky. You're declaring a class inside the partial class of the page. Try setting up your code like this:

public partial class ShippedOrders : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.goFiles();
    }

    public void goFiles()
    {
        string[] array1 = Directory.GetFiles(@"C:\Kaplan\Replies\");
        string[] array2 = Directory.GetFiles(@"C:\Kaplan\Replies\", "*.REP");
        System.IO.StreamReader file = null;
        string line = "";
        bool hasLIFT = false;

        Response.Write("---Files:---<br/>");
        foreach (string name in array1)
        {
            file = new System.IO.StreamReader(@name);
            while((line = file.ReadLine()) != null)
            {
                if(line.StartsWith("LIFT"))
                {
                    hasLIFT = true;
                    break;
                }
             }
             if(hasLIFT)
             {
                 Response.Write("<span style=\"color:Red;\">" + name + "</span><br/>";
                 hasLIFT = false;
             }
             else
                 Response.Write(name + "<br/>";
        }
        //and can do the same for other array
    }
}

Upvotes: 2

7heViking
7heViking

Reputation: 7577

You can do like this inside your foreach loop: if(name.contains("LIFT")) { //make red. }

it does though have the issue that it only checks if the string (name) contains the string LIFT, and not if the string is in the beginning of the filename. If you want to check if LIFT is in the beggining of the file name you must use some of the Trim methods.

Upvotes: 0

Related Questions