非静态字段,方法或属性需要对象引用

时间:2012-08-14 17:15:51

标签: c#

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

目标文件夹包含 d:\测试\ test1.txt的 的test2.txt test3.txt

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

这不是完整的代码。但我收到错误非静态字段,方法或属性需要对象引用。 at 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;
    }
}

2 个答案:

答案 0 :(得分:6)

Main()静态方法 它与任何实例都没有关联。

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

答案 1 :(得分:0)

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

变化

while (!getFileName...

Program p = new Program();
while (!p.getFileName...