您能帮助我如何在C#.NET中编写代码以计算时间吗 [英] Can u help me how to write code in C# .NET for Calculating the Time

查看:98
本文介绍了您能帮助我如何在C#.NET中编写代码以计算时间吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在C#中的代码,搜索结果的时间显示为0.0.00 ...我想计算每个搜索到的文件的时间...

如果您能帮助我,我的项目将大有帮助....
谢谢您……...


PLZZZ HELP ME ....:confused:

对于下面的代码,我在时间列中得到的结果为0.0.00..
我想在那个地方计算时间...


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...


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);
                }
            }
        }
    }



[修改:添加了< pre>标签]



[Modified: added <pre> tags]

推荐答案

"
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.


您真的认为有人会仔细阅读该网站的整个代码吗?你的?我想格式化它,但是看到了长度,我也把它留了下来!

我建议您:
1.放置适当的问题说明,仅包含特定的代码片段,如果需要的话也可以
2.不要使用CAPS,好像您在喊什么
3.制定看起来像问题/无法解决的问题.

完成后,请确保很多人会很乐意查找/尝试解决您的问题! :thumbsup:
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:


是的...要看的代码很多.您是否尝试单步执行代码以查看问题所在?

好吧,我可以不告诉你,但这就是你下次应该做的.您似乎想要显示创建/修改文件与查看该文件的时间之间的时间差.因此,这很好,但是如果您想要的话,您实际上从文件中根本没有得到时间.

这是您编写的基本顺序:

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:

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



如果在代码运行时间startTime和StopTime之间花费的时间,这将为您提供时间,根据您的代码,代码将仅花费几毫秒,因此为0小时,0分钟,0秒或"0.0.00".

另外,您应该意识到,如果您有一个类似
的块



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

If (something == true)
{
    //do something
}
else
{
    //do something else
}


在if部分中声明的变量在else部分中不可用.

在你的其他人中,你有


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

In your else, you have

_StopTime = DateTime.Now;
_Difference = (_StopTime - _StartTime);



由于隐式转换会起作用,但是良好的编码习惯是在适当的范围内声明这些变量.


[更新]
Sandeep回答了您有关毫秒的问题.但是,严重的是,您仅运行大约十二个操作,并且您想知道这需要花费多长时间?有什么意义?



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?


这篇关于您能帮助我如何在C#.NET中编写代码以计算时间吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆