C# 异常:当用户输入C:\Name时,不支持给定的路径格式
我制作了一个小型控制台应用程序,帮助用户将所有内容从一个目录复制到另一个目录。代码如下:C# 异常:当用户输入C:\Name时,不支持给定的路径格式,c#,console,C#,Console,我制作了一个小型控制台应用程序,帮助用户将所有内容从一个目录复制到另一个目录。代码如下: using System; using System.IO; namespace CopyEdriveContentToLocalFolder { class Program { public static void Copy(string sourceDirectory, string targetDirectory) { Direc
using System;
using System.IO;
namespace CopyEdriveContentToLocalFolder
{
class Program
{
public static void Copy(string sourceDirectory, string targetDirectory)
{
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
CopyAll(diSource, diTarget);
}
public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
Directory.CreateDirectory(target.FullName);
// Copy each file into the new directory.
foreach (FileInfo fi in source.GetFiles())
{
Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
static void Main(string[] args)
{
// Specify a name for your Parent folder.
string ParentFolderName;
string ChildFolderName;
Console.WriteLine("Welcome!!\n\nThis program will help you copy all content of your CD/DVD to a new directry \n\n" +
"naming convention must be in this format: ### #####\n\nPress Enter to start...\n");
while (Console.ReadKey(true).Key != ConsoleKey.Escape)
{
Console.Write("Enter Parent folder Name: ");
ParentFolderName = Console.ReadLine();
string text = ParentFolderName;
string contains = " ";
bool b = text.Contains(contains);
if ( b == true)
{
//Set the Child folder name to be same as Parent.
ChildFolderName = ParentFolderName;
string TopLevel = @"" + ParentFolderName;
if (!System.IO.Directory.Exists(TopLevel))
{
string pathString = System.IO.Path.Combine(TopLevel, ChildFolderName);
System.IO.Directory.CreateDirectory(pathString);
}
else
{
Console.WriteLine("The Folder: \"{0}\" already exists.\n\n", TopLevel);
Console.WriteLine("Please Exit the program and re-name the folder .\n\n");
System.Console.WriteLine("Press Enter twice to exit.");
System.Console.ReadKey();
return;
}
// To create a string that specifies the path to a subfolder under your
// top-level folder, add a name for the subfolder to folderName.
//string pathString2 = @"c:\"+ ParentFolderName +"\\" + ChildFolderName;
// You can extend the depth of your path if you want to.
//pathString = System.IO.Path.Combine(pathString, "SubSubFolder");
string sourceDirectory = @"E:\";
string targetDirectory = TopLevel +"\\" + ChildFolderName;
Copy(sourceDirectory, targetDirectory);
string root = TopLevel + "\\" + ChildFolderName + "\\System Volume Information";
//This part will delete the System generated folder.
// If directory does not exist, don't even try
if (Directory.Exists(root))
{
foreach (string files in Directory.GetFiles(root))
{
FileInfo fileInfo = new FileInfo(files);
fileInfo.Delete(); //delete the files first.
}
Directory.Delete(root);
}
// Keep the console window open in debug mode.
System.Console.Write("All Neccery folders have been copied..\n\n" + "Pleace check the target directory.\n\n" +
"Hit Escape twice to exit or Enter to continue.\n");
// System.Console.ReadKey();
}
else
{
//Console.WriteLine("The Folder: \"{0}\" already exists.\n\n", TopLevel);
Console.WriteLine("\nAn Error has occurred due to NOT following the naming convention.\n\n");
Console.WriteLine("Please Make sure the Folder name correct\n\nExit the program and restart.\n\n");
System.Console.WriteLine("Press Enter twice to exit.");
System.Console.ReadKey();
return;
}
}
}
}
}
我在这一行得到的格式错误的例外:
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
如果我把驱动器硬编码为@“C:\”,它就可以正常工作
string targetDirectory = @"c:\" + ParentFolderName + "\\" + ChildFolderName;
谁能帮我一下吗。我需要将目标目录和源代码设置为完全由用户决定。
谢谢试试这个:
string targetDirectory = @"c:\" + ParentFolderName + "\" + ChildFolderName;
或者这个:
string targetDirectory = "c:\\" + ParentFolderName + "\\" + ChildFolderName;
问题是“\”在字符串中用作转义字符。当用户输入D:\C#时,不会将其视为路径。因此,DirectoryInfo diTarget=newdirectoryinfo(targetDirectory)代码>引发无法识别路径的异常,因为字符串targetDirectory
的路径值不正确。为了解决这个问题,我们采取以下措施:
Console.Write("Enter Parent folder Name: ");
path = @"" + Console.ReadLine();
这样,用户可以输入任何目录。Ex)D:\测试
在我的代码中,它通过说ChildFolderName=ParentFolderName来创建父目录和子目录代码>现在的问题是我不能有D:\testD:\test的路径
要解决这个问题,您需要创建另一个存储用户输入的变量-first 3 char并将其分配给childFolderName
path = @"" + Console.ReadLine();
ParentFolderName = path.Substring(3);
ChildFolderName = ParentFolderName;
以及:
如果您遇到异常,您需要。并找出什么是targetDirectory
。@DourHighArch异常在标题中,目标目录是用户输入的,如问题和代码注释所示。抱歉代码太长,但希望有一天其他人会使用它。请阅读我发布的链接;您的标题仅包含例外情况中的几个词,不足以让我们说出问题所在。我猜用户没有访问文件夹或目录的权限,或者选择了无效的名称。一般来说,让文件路径“完全由用户决定”是个坏主意,因为文件路径的规则是。我同意起初驱动器是硬编码的,但我的领导希望由用户来设置起始和结束。我解决了这个问题,我将很快发布答案。string targetDirectory=Path.Combine(“c:\”,ParentFolderName,ChildFolderName)代码>谢谢Dmitry:)谢谢大家的回复,我知道我可以按照问题中提到的方式让它工作。我的问题是我希望用户能够输入C:\或D:\我不希望它硬编码。我发现这是一个解决方案:Console.Write(“输入父文件夹名称:”);ParentFolderName=@“+Console.ReadLine();这意味着,如果您引用代码,我不能让父值和子值与我现在所做的相同。请记住,您的用户可能无法控制驱动器名。@CountLessQConsole.ReadLine()?你偏离了轨道。
string sourceDirectory = @"E:\";
string targetDirectory = path + "\\" + ChildFolderName;