在函数C#中使用控件 [英] Using control in functions C#

查看:86
本文介绍了在函数C#中使用控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..

以下是我的代码:-

Hi..

Following is my code:-

public static void Sync(string sourcePath, string destinationPath)
       {
           bool dirExisted = DirExists(destinationPath);

           //get the source files
           string[] srcFiles = Directory.GetFiles(sourcePath);

           foreach (string sourceFile in srcFiles)
           {
               FileInfo sourceInfo = new FileInfo(sourceFile);
               string destFile = Path.Combine(destinationPath, sourceInfo.Name);

               if (!dirExisted && File.Exists(destFile))
               {
                   FileInfo destInfo = new FileInfo(destFile);
                   if (sourceInfo.LastWriteTime > destInfo.LastWriteTime)
                   {
                       //file is newer, so copy it
                       File.Copy(sourceFile, Path.Combine(destinationPath, sourceInfo.Name), true);

                      MessageBox.Show(sourceFile + "copied(newer version)");




                   }
               }

               else
               {
                   File.Copy(sourceFile,Path.Combine(destinationPath,sourceInfo.Name));
                   MessageBox.Show(sourceFile + "copied");
               }
           }

           DeleteOldDestinationFiles(srcFiles, destinationPath);

           //now process the directories if exist
           string[] dirs = Directory.GetDirectories(sourcePath);

           foreach (string dir in dirs)
           {
               DirectoryInfo dirInfo = new DirectoryInfo(dir);

               //recursive do the directories
               Sync(dir, Path.Combine(destinationPath, dirInfo.Name));
           }
       }


我想使用ToolStripStatus标签代替MessageBoxes..

有人可以帮我吗?

谢谢..


here instead of MessageBoxes i want to use ToolStripStatus Label..

can anyone help me with this??

Thanks..

推荐答案

尝试:
myStatus.Text = sourceFile + "copied";

仅在单词copied
前面加一个空格

出现以下错误:-非静态字段,方法或属性" SplashScreen.Form1.toolStripStatusLabel1"需要对象引用"

好的.这很难解释...

您正在尝试访问通用标签,而不是特定标签,因此它在抱怨您需要对象引用.为什么?因为您的方法被声明为static,所以它没有this引用.如果使用静态方法,则它只能访问其参数和包含类的静态部分,而不能访问常规"实例值.

这有点像将手机放入车载杂物箱,然后期望在您尝试打开的任何车载杂物箱中找到它.它不会发生-您必须参考放入电话的汽车的特定实例才能取回它.

从您的方法中删除静态声明,它应该可以工作.

Only I would include a space in front of the word copied


"I get the following error :- An object reference is required for the non-static field, method, or property ''SplashScreen.Form1.toolStripStatusLabel1''"

OK. This isn''t going to be easy to explain...

You are trying to access a generic Label, instead of a specific label, so it is complaining that you need an object reference. Why? Because your method is declared as static so it does not have a this reference. If you use a static method, it can only access it''s parameters and static parts of the containing class - it can''t access "normal" instance values.

It''s a bit like putting your phone in your car glove box, and then expecting to find it in any car glove box you try to open. It doesn''t happen - you have to refer to the specific instance of a car that you put the phone in to retrieve it.

Remove the static declaration from your method, and it should work.


这篇关于在函数C#中使用控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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