在C#控制台应用程序中使用File.Copy(源、目标、true)找不到路径的一部分
我继续获得路径“C:\Users(user profile)\VirtualStore\Program Files(x86)\E!PC\Macros)”的一部分(找不到)异常。该目录在驱动器上,但我不知道为什么我继续得到这个异常在C#控制台应用程序中使用File.Copy(源、目标、true)找不到路径的一部分,file,c#-4.0,exception,path,copy,File,C# 4.0,Exception,Path,Copy,我继续获得路径“C:\Users(user profile)\VirtualStore\Program Files(x86)\E!PC\Macros)”的一部分(找不到)异常。该目录在驱动器上,但我不知道为什么我继续得到这个异常 Extra6DestPath = "C:\Users\(user profile)\VirtualStore\Program Files (x86)\E!PC\Macros\" static void copyMacrosAndBitmaps(string Extra
Extra6DestPath = "C:\Users\(user profile)\VirtualStore\Program Files (x86)\E!PC\Macros\"
static void copyMacrosAndBitmaps(string ExtraSourcePath, string Extra6xDestPath )
{
//counter for total Macro count on network
int Count = 0;
//counter for total bitmap count on network
int iCount = 0;
//Get File information to use for copy
FileInfo[] macrosArray;
FileInfo[] iconArray;
//Get Directory information to use for copy
DirectoryInfo di = new DirectoryInfo(ExtraSourcePath);
DirectoryInfo diIcon = new DirectoryInfo(ExtraIconPath);
//set all macro paths as a string from directory into an array
macrosArray = di.GetFiles("*.ebm");
Count = macrosArray.Length;
//set all bitmaps from directory into an array
iconArray = diIcon.GetFiles("*.bmp");
iCount = iconArray.Length;
//copy macros into destination folder
if (Count == 0)
{
throw new FileNotFoundException("No Macros found to copy");
}
else
{
for (int i = 0; i < Count; i++)
{
File.Copy(Extra6xSourcePathW7 + macrosArray[i].ToString(), Extra6xDestPath + iconArray[i].Name, true);
}
//Copy the bitmaps into destination folder
if (iCount == 0)
{
throw new FileNotFoundException("No bitmaps found to copy");
}
else
{
for (int i = 0; i < Count; i++)
{
File.Copy(ExtraIconPath + iconArray[i].ToString(), Extra6xDestPath + iconArray[i].Name, true);
}
}
}
}
Extra6DestPath=“C:\Users\(用户配置文件)\VirtualStore\Program Files(x86)\E!PC\Macros\”
静态void CopyMacrosandBitmap(字符串ExtraSourcePath、字符串Extra6xDestPath)
{
//网络上总宏计数的计数器
整数计数=0;
//网络上位图总计数的计数器
int-iCount=0;
//获取用于复制的文件信息
FileInfo[]宏数组;
FileInfo[]iconArray;
//获取用于复制的目录信息
DirectoryInfo di=新的DirectoryInfo(extracourcepath);
DirectoryInfo diIcon=新的DirectoryInfo(ExtraIconPath);
//将所有宏路径设置为从目录到数组的字符串
宏数组=di.GetFiles(“*.ebm”);
Count=宏数组。长度;
//将目录中的所有位图设置为数组
iconArray=diIcon.GetFiles(“*.bmp”);
iCount=iconArray.Length;
//将宏复制到目标文件夹中
如果(计数=0)
{
抛出新的FileNotFoundException(“未找到要复制的宏”);
}
其他的
{
for(int i=0;i
我首先尝试用@symbol声明路径,以处理需要转义的字符:
Extra6DestPath = @"C:\Users\(user profile)\VirtualStore\Program Files (x86)\E!PC\Macros\"
Copy(Extra6xSourcePathW7+macrosArray[i].ToString(),Extra6xDestPath+macrosArray[i].name,true);我错过了API中的目标不能是目录而必须是特定文件名的部分。希望这至少能帮助其他人从我看这本书时的头痛中解脱出来!!以上已编辑的代码是正确运行的版本!!祝大家编程愉快!!你是对的。。。这就是我应该怎么做的,但是为了简单起见,我把我的值保存在App.Config中,并且只显示为硬编码(并且忘记了@)。不过我确实知道我在做什么。我试图传递一个目录作为目标,但是File.Copy方法在第二个字符串目标中需要的是实际路径和要存储为的文件名。由于我想保持相同的文件名,所以我只引用了索引I处FileInfo数组的名称atribute,并将其关联到我已经使用的路径。谢谢你对我问题的快速回答!