c# 标识符预期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1225214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:53:13  来源:igfitidea点击:

c# identifier expected?

c#

提问by Steven

I am trying to create a program to copy all the files from one directory to another. But I am running in a basic issue. It says indentifier expected when I try to compile on line 52.

我正在尝试创建一个程序来将所有文件从一个目录复制到另一个目录。但是我遇到了一个基本问题。当我尝试在第 52 行编译时,它表示需要使用标识符。

public bool RecursiveCopy()
{
    string origDir = @"D:\Documents and Settings\Dub\My Documents\HoN Updates\test";
    string destDir = @"C:\Games\HoN";
    bool status = false;

    //get all the info about the original directory
    var dirInfo = new DirectoryInfo(origDir);

    //retrieve all the _fileNames in the original directory
    var files = dirInfo.GetFiles(origDir);

    //always use a try...catch to deal 
    //with any exceptions that may occur
    try
    {
        //loop through all the file names and copy them
        foreach (string file in Directory.GetFiles(origDir))
        {
            var origFile = new FileInfo(file);
            var destFile = new FileInfo(file.Replace(origDir, destDir));

            //copy the file, use the OverWrite overload to overwrite
            //destination file if it exists

            System.IO.File.Copy(origFile.FullName, destFile.FullName, true);

            //TODO: If you dont want to remove the original
            //_fileNames comment this line out
            File.Delete(origFile.FullName);
            status = true;
        }
        Console.WriteLine("All files in " + origDir + " copied successfully!");
    }
    catch (Exception ex)
    {
        status = false;

        //handle any errors that may have occurred
        Console.WriteLine(ex.Message);
    }
    return status;
}

public string origDir = @"D:\Documents and Settings\Dub\My Documents\HoN Updates\test"; // ERROR HERE
public string destDir = @"C:\Games\HoN"; // ERROR HERE

private static void RecursiveCopy(origDir, destDir)
{
    Console.WriteLine("done");
    Console.ReadLine();
}

采纳答案by Ed S.

You did not give type identifiers to your argument list here

您没有在此处为参数列表提供类型标识符

static void RecursiveCopy(origDir, destDir)

should be

应该

static void RecursiveCopy(string origDir, string destDir)

回答by Sam Harwell

Your method RecursiveCopyhas two parameters listed without their types. It should be this:

您的方法RecursiveCopy列出了两个没有类型的参数。应该是这样的:

static void RecursiveCopy(string origDir, string destDir)

回答by Daniel Brückner

cYou are missing the parameter types in the RecursiveCopymethod declaration. Just Change

c您在RecursiveCopy方法声明中缺少参数类型。只是改变

static void RecursiveCopy(origDir, destDir)

to

static void RecursiveCopy(String origDir, String destDir)

and all is fine.

一切都很好。

回答by lc.

Here is your problem:

这是你的问题:

static void RecursiveCopy(origDir, destDir)

You don't specify the types for the parameters, perhaps you intended the following:

您没有指定参数的类型,也许您打算执行以下操作:

static void RecursiveCopy(string origDir, string destDir)


There are more issues however that I've noticed. It's possible you're still working on these, but from what you've posted:

但是,我注意到还有更多问题。您可能仍在处理这些问题,但从您发布的内容来看:

  • You never call your RecursiveCopymethod. Perhaps you meant to call it from Main()instead of declaring an overload with two parameters?

  • You declare two public fields origDirand destDirbut then never use them. Instead you create two local variables in RecursiveCopy()and use these instead. Did you intend to create parameters or use the public fields instead?

  • Your copy is not actually true to its name of "recursive".

  • 你永远不会调用你的RecursiveCopy方法。也许您打算调用它Main()而不是声明带有两个参数的重载?

  • 您声明了两个公共字段origDirdestDir但从不使用它们。相反,您可以在其中创建两个局部变量RecursiveCopy()并使用它们。您打算创建参数还是使用公共字段?

  • 您的副本实际上并不符合其“递归”的名称。