无法调用 Main() 中的方法?

Unable to call method in Main()?

你好,我正在学习 C#。

尝试在实例中调用我的方法时,我没有获得智能选项,因此不得不输入它。

我在尝试构建时也遇到了当前的两个错误:

1.)"'string[]' 不包含 'LastWriteTime' 的定义,并且找不到接受类型 'string[]' 的第一个参数的扩展方法 'LastWriteTime'。"

2.)"Member 'MyClass.Move_Modified_Files()' cannot be accessed with an instance reference; qualify it with a type name instead"

任何线索将不胜感激!

这是我的代码:

namespace File_Mover
{
    public class MyClass
    {
        public static string src = @"C:\Users\Bold Defiance\Desktop\FolderA";
        public static string dst = @"C:\Users\Bold Defiance\Desktop\FolderB";
       public static string[] files = System.IO.Directory.GetFiles(src, "*.txt");

        public static void Move_Modified_Files()
        {
            try
            {
                if (files.LastWriteTime.Date == DateTime.Today.Date)
                {
                    File.Move(src, dst);
                    Console.WriteLine("Modified files in {0} were moved to {1}", src, dst);
                }
                else
                {
                    Console.WriteLine("No new or modified files were created today.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyClass cls = new MyClass();
            cls.Move_Modified_Files();
        }
    }
}

修正错误:

1.)System.IO.Directory.GetFiles returns 一组文件路径。如果要检查每个文件的最后写入时间,可以使用 System.IO.File.GetLastWriteTime() 传递完整路径。您必须遍历数组才能完成此操作。

2.) 您的方法和成员变量在 class 中是静态的,不需要是静态的。您应该使该方法成为非静态方法,然后这个 error/warning 就会消失。