Click here to Skip to main content
15,913,335 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
THIS IS MY CODE IN C# and the time for the search result is showing as 0.0.00......i want to calculate the time for each and every file searched...

IT WOULD BE SO HELPFULL FOR MY PROJECT IF U HELP ME....
THANK YOU IN ADVANCE.........


PLZZZ HELP ME....:confused:

For below code i am getting the result as 0.0.00 in the time column..
i want the time to be calculated in that place...


C#
public void RecursiveSearch(string url, int depth)
{
    DirectoryInfo di = new DirectoryInfo(url);
    if (!di.Exists)
        return;

    if (depth > Settings.searchDepth)
        return;
    depth++;

    SetSearchStatus("Searching " + url);

    if (Search.filters == null)
    {
        if (di.Name.ToLower().Contains(Search.keyword))
        {
            string dirname;
            if (di.Parent == null)
                dirname = di.FullName.Substring(0, di.FullName.LastIndexOf(@"\"));
            else
                dirname = di.Parent.FullName;
            AddRow(di.Name, dirname, "-");
            /*Hmm parent directory ka panga*/
        }
    }

    foreach (FileInfo fi in di.GetFiles())
    {
        DateTime startTime = DateTime.Now;

        string filename = fi.Name.ToLower();

        if (filename.Contains(Search.keyword))
        {
            if (Search.filters != null)
            {
                foreach (string s in Search.filters)
                {


                    if (filename.EndsWith(s))
                    {
                        if (fi.Length > Search.minsize && (Search.maxsize == -1
                            || fi.Length < Search.maxsize))
                        {
                            long length = fi.Length;
                            string slen;
                            if (length > 1073741824)
                            {
                                slen = (length / 1073741824) + " GB";
                            }
                            else
                            {
                                if (length > 1048576)
                                {
                                    slen = (length / 1048576) + " MB";
                                }
                                else
                                {
                                    if (length > 1024)
                                    {
                                        slen = (length / 1024) + " KB";
                                    }
                                    else
                                        slen = length + " B";
                                }
                            }

                            DateTime stopTime = DateTime.Now;


                            TimeSpan _Difference = (_StopTime - _StartTime);
                            AddRow(fi.Name, di.FullName, slen, _Difference);

                        }
                        break;
                    }
                }
            }
            else
            {
                if (fi.Length > Search.minsize && (Search.maxsize == -1
                    || fi.Length < Search.maxsize))
                {

                    _StartTime = DateTime.Now;
                    long length = fi.Length;
                    string slen;
                    if (length > 1073741824)
                    {
                        slen = (length / 1073741824) + " GB";
                    }
                    else
                    {
                        if (length > 1048576)
                        {
                            slen = (length / 1048576) + " MB";
                        }
                        else
                        {
                            if (length > 1024)
                            {
                                slen = (length / 1024) + " KB";
                            }
                            else
                                slen = length + " B";
                        }
                    }

                    _StopTime = DateTime.Now;

                    _Difference = (_StopTime - _StartTime);

                    AddRow(fi.Name, di.FullName, slen, _Difference);
                }
            }
        }
    }


[Modified: added <pre> tags]
Posted
Updated 26-Mar-10 10:05am
v3

"
SQL
I need to get in Milliseconds..

for that what must i do..

"

You are an idiot. I told you not to push 'answer' to add more comments. Others have explained to you that within the granularity of the objects you're using, the time taken of the operation you're timing is so quick that you can't time it. So, you've been answered, you've been told how to use the site, but you keep ignoring us.
 
Share this answer
 
Do you really think anyone would go through that whole code of yours? I thought of formatting it but seeing the length, i left that too!

I would suggest you to:
1. put a proper problem statement with only specific code snippet that too if needed
2. Dont use CAPS, it looks like you are shouting
3. Formulate what was done by you that looks like an issue/not working.

Once done, be sure lot of people will be happy looking/trying to resolve your problem! :thumbsup:
 
Share this answer
 
Yeah...that's a lot of code to look through. Did you try stepping through the code to see what you're problem is?

Well, I can tell you without doing that, but that's what you should do next time. It looks like you want to show the difference in time between when the file was created/modified and the time the person is looking at it. So, that's fine, but you never actually get the time from the File if that's what you're wanting.

Here's the basic sequence that you've written:

C#
DateTime startTime = DateTime.Now;
//more code
DateTime stopTime = DateTime.Now;
TimeSpan _Difference = (_StopTime - _StartTime);
AddRow(fi.Name, di.FullName, slen, _Difference);


All that will give you is the time if took to run the code between the startTime and StopTime, which based on your code will only take a few milliseconds, therefore 0 hours, 0 minutes, 0 seconds or "0.0.00".

Also, you should realize that if you have a block like
C#
If (something == true)
{
    //do something
}
else
{
    //do something else
}

variables declared within the If section are not available in the else section.

In your else, you have
C#
_StopTime = DateTime.Now;
_Difference = (_StopTime - _StartTime);


that will work because of implicit conversion, but good coding practice is to declare those variables in the proper scope.


[Update]
Sandeep answered your question about milliseconds. But, seriously, you're running only about a dozen operations and you want to see how long that takes? What's the point?
 
Share this answer
 
v2
You appear to be measuring the duration of a very small section of code and it would be suprise if the result was greater than zero. It's only the I/O call to Directory.GetFiles that is likely to take a significant amount of time. Everything else you have shown is just simple processing of the results and will be very fast.

Alan.
 
Share this answer
 
Now,
madhu163 wrote:
search result is showing as 0 : 0: 00

If you are aware, TimeSpan object string will show hh:mm:ss (hour:minutes:seconds)
Since you are getting it 0 : 0 :00 this means its taking less than a second (very quick search). I would suggest to start with Ticks for verification.
Also you can try TotalMilliseconds like:
C#
TimeSpan timeDifference = (stopTime - startTime);
Console.Write(timeDifference.TotalMilliseconds);
// To check you really are getting some value for time difference
Console.Write(timeDifference.Ticks)
 
Share this answer
 
<removed>

Please don't push 'answer' to ask more questions, edit your post

Please don't post pages and pages of code, no-one is going to read it all

Please don't tell us how urgent your issue is, that your business plan is to wait until the last minute and then ask random strangers to do your work, does not make it urgent for us

Please read the answers you got, the issue is that the gap of time you're trying to measure is so small that 0:00:00 is almost certainly the correct answer.
 
Share this answer
 
v2
I need to get in Milliseconds..

for that what must i do..

UPDATE:
SM: Looks like you had not read my reply. I already showed you how to get that in Milliseconds here:
http://www.codeproject.com/answers/68822/Can-u-help-me-how-to-write-code-in-Csharp-NET-for-.aspx#answer2[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900