egametang/ET · error · Exception

父目录不能拷贝到子目录!

Error message

父目录不能拷贝到子目录!

What it means

Thrown by FileHelper.CopyDirectory when the target directory is inside the source directory. Copying a parent into its own child would recurse infinitely (the copy would keep copying itself into the copy), so it is rejected up front.

Source

Thrown at Packages/cn.etetet.core/Scripts/Core/Share/Helper/FileHelper.cs:55

			foreach (string subdir in Directory.GetDirectories(dir))
			{
				Directory.Delete(subdir, true);		
			}

			foreach (string subFile in Directory.GetFiles(dir))
			{
				File.Delete(subFile);
			}
		}

		public static void CopyDirectory(string srcDir, string tgtDir)
		{
			DirectoryInfo source = new DirectoryInfo(srcDir);
			DirectoryInfo target = new DirectoryInfo(tgtDir);
	
			if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase))
			{
				throw new Exception("父目录不能拷贝到子目录!");
			}
	
			if (!source.Exists)
			{
				return;
			}
	
			if (!target.Exists)
			{
				target.Create();
			}
	
			FileInfo[] files = source.GetFiles();
	
			for (int i = 0; i < files.Length; i++)
			{
				File.Copy(files[i].FullName, Path.Combine(target.FullName, files[i].Name), true);
			}

View on GitHub (pinned to 5cab01f7a8)

Solutions

  1. Place the target directory outside the source tree entirely (sibling, not child).
  2. If output must nest, copy to a temp dir outside source first, then move into place.
  3. Validate `!target.FullName.StartsWith(source.FullName)` in your own caller before invoking CopyDirectory.

Example fix

// before
FileHelper.CopyDirectory(src, Path.Combine(src, "out"));   // out is inside src

// after
FileHelper.CopyDirectory(src, Path.Combine(parentOfSrc, "out"));
Defensive patterns

Strategy: validation

Validate before calling

var src = new DirectoryInfo(srcDir).FullName;
var tgt = new DirectoryInfo(tgtDir).FullName;
if (!tgt.StartsWith(src, StringComparison.OrdinalIgnoreCase))
{
    FileHelper.CopyDirectory(srcDir, tgtDir);
}

Prevention

When it happens

Trigger: Passing srcDir as an ancestor of tgtDir; backup/copy tooling that writes the output under the source tree; build scripts whose output dir nests under the input.

Common situations: Generating output into a subfolder of the input; deploy/copy scripts with reversed or misnested paths; tooling that defaults output to ./input/out.

Related errors


AI-assisted analysis of egametang/ET@5cab01f7a8 (2026-08-13). Data as JSON: /api/errors/dbf669faa1435367. Report an issue: GitHub.