Chee mun Low
Chee mun Low

Reputation: 83

c# failed to check existed file in directory

there are nothing is inside my test002 folder, by right my output should be is "nothing is inside the folder",but after compile there are nothing prompt.

what i want to do is if there are any .doc file is inside my folder, just upload it if there are nothing is inside the folder, ask user to upload .doc to required folder.

protected void Button3_Click(object sender, EventArgs e)
{
    try
    {
        string[] chkUserResume = Directory.GetFiles(HttpContext.Current.Server.MapPath(@"~/Enduser/test002/"), "*.doc");

        if (chkUserResume!=null)
        {

            foreach (string name in chkUserResume)
            {
                Response.Write(name + " is exist");
            }
        }
        else
        {
            Response.Write("nothing is inside the folder");
        }



    }
    catch (Exception ex) 
    { 
        Response.Write(ex.Message.ToString());
    }


}

Upvotes: 0

Views: 135

Answers (2)

ChrisF
ChrisF

Reputation: 137188

You're not checking that chkUserResume is empty:

    if (chkUserResume.Length == 0)
    {
        Response.Write("nothing is inside the folder");
    }
    else
    {
        foreach (string name in chkUserResume)
        {
            Response.Write(name + " is exist");
        }
    }

However as chkUserResume will never be null there's no need to check for that.

Upvotes: 1

Polynomial
Polynomial

Reputation: 28346

The null keyword means that the variable is not set to any real value, which is different from an empty array.

In this case, chkUserResume will never be null, it will be an empty array. You should check that chkUserResume.Length is 0 instead.

Upvotes: 3

Related Questions