0

您好我正在尝试编写一个将文件从源文件夹复制到目标文件夹的代码。如果目标文件夹包含相同的文件名,那么我的程序应该存储不同名称的文件。例如源文件夹包含:C:\test\ test1.txt test2.txt

目标文件夹包含 D:\test\ test1.txt test2.txt test3.txt

然后复制操作应将 test1.txt 和 test2.txt 从源文件夹复制到目标文件夹,并将名称更改为 test4.txt 和 test5.txt

这不是完整的代码。但我收到错误非静态字段、方法或属性需要对象引用。在 getFileName(ref destfileName, ref targetPath)。对此有什么帮助吗?

class Program
{
    static void Main(string[] args)
    {
        string sourcefileName = null;
        string destfileName = null;
        string sourcePath = @"C:\test";
        string targetPath = @"D:\test";
        List<int> seqNum = new List<int>();

        // To copy a folder's contents to a new location:
        // Create a new target folder, if necessary.
        if (!System.IO.Directory.Exists(targetPath))
        {
            System.IO.Directory.CreateDirectory(targetPath);
        }

        // To copy all the files in one directory to another directory.
        // Get the files in the source folder. (To recursively iterate through
        // all subfolders under the current directory, see
        // "How to: Iterate Through a Directory Tree.")
        // Note: Check for target path was performed previously
        //       in this code example.
        if (System.IO.Directory.Exists(sourcePath))
        {
            string[] files = System.IO.Directory.GetFiles(sourcePath);

            // Copy the files and overwrite destination files if they already exist.
            foreach (string s in files)
            {
                // Use static Path methods to extract only the file name from the path.
                //File name is like text1.txt
                sourcefileName = System.IO.Path.GetFileName(s);       
                if (System.IO.Directory.GetFiles(targetPath).Count() > 0)
                {
                    foreach (string file in System.IO.Directory.GetFiles(targetPath))
                    {
                        if (file.Contains(sourcefileName))
                        {
                            int num;
                            string existingLatestFile = string.Empty;
                            destfileName = sourcefileName.Replace(".txt", string.Empty);
                            for (int i = 0; i < sourcefileName.Length; i++)
                            {
                                if (Char.IsDigit(sourcefileName[i]))
                                {
                                    existingLatestFile += sourcefileName[i];
                                }
                            }
                            if (int.TryParse(existingLatestFile, out num))
                            {
                                seqNum.Add(num);
                            }
                            destfileName = destfileName.Replace(existingLatestFile, string.Empty);//Remove existing number
                            num = num + 1;
                            destfileName = destfileName + num.ToString() + ".txt"; // Make a new file name
                            while (!getFileName( ref destfileName, ref targetPath))
                            {

                            }

                        }
                        else
                        {
                            destfileName = sourcefileName;
                        }
                        string destFile = System.IO.Path.Combine(targetPath, destfileName);
                        System.IO.File.Copy(s, destFile, false);
                    }
                }

            }
        }
        else
        {
            Console.WriteLine("Source path does not exist!");
        }

        if (System.IO.Directory.GetFiles(targetPath).Count() > 0)
        {
            foreach (string file in System.IO.Directory.GetFiles(targetPath))
            {
        /*        if (file.Contains(dir + "\\" + filename))
                {
                    int num;
                    existingLatestFile = file.Replace(dir + "\\" + filename, string.Empty);
                    existingLatestFile = existingLatestFile.Replace(".txt", string.Empty);

                    if (int.TryParse(existingLatestFile, out num))
                    {
                        seqNum.Add(num);
                    }
              }*/
          Console.WriteLine(file);
            }
        }

        // Keep console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }

    bool getFileName(ref string filename, ref string destFolder)
    {
        bool retValue =false;
        foreach (string file in System.IO.Directory.GetFiles(destFolder))
        {
            if (file.Contains(filename))
            {
                retValue = false;
            }
            else
            {
                retValue = true;
            }                
        }           
        return retValue;
    }
}
4

2 回答 2

6

Main()是一种静态方法。
它不与任何实例关联。

您还需要将其他方法设为静态。

于 2012-08-14T17:18:35.250 回答
0

main 是一个静态方法,要调用非静态方法 getFileName 需要先创建一个实例

改变

while (!getFileName...

Program p = new Program();
while (!p.getFileName...
于 2012-08-14T17:21:21.577 回答