文件拷贝后变成这样了(请各位支支招) 我用BinaryWriter 和FileStream 拷贝u盘的文件到目录 出现这样的情况 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 U盘里有这个图片(B-PNG_LI-26.png,B-JPEG_M-19.jpeg )拷贝到目录后变成了B-PNG_LI-26.png,B-JPEG_M-19.jpeg ..B-PNG_LI-26.png.jpg,...B-PNG_LI-26.png.jpg.jpg,...B-PNG_LI-26.png.jpg.jpg.jpg,....B-PNG_LI-26.png.jpg.jpg.jpg.jpg,"..B-PNG_LI-26.png.jpg,...B-PNG_LI-26.png.jpg.jpg,...B-PNG_LI-26.png.jpg.jpg.jpg,....B-PNG_LI-26.png.jpg.jpg.jpg.jpg,"这些都是隐藏的 并且大小也要比B-PNG_LI-26.png小一些 if (!File.Exists(source_file)) { return; } byte[] bytTemp = new byte[4096];//字节数组 //long lngHad = 0; long lngHad = _currentSize; long lngCount; int z = 5000; //源文件流 System.IO.FileStream fsSource = new System.IO.FileStream(source_file, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read, 4); //二进制读取器 System.IO.BinaryReader bRead = new System.IO.BinaryReader(fsSource); //定位源文件流的头部 bRead.BaseStream.Seek(0, System.IO.SeekOrigin.Begin); if (fsSource.Position > 0) fsSource.Position = 0; lngCount = _totalSize; //目标文件流 System.IO.FileStream fsAim = new System.IO.FileStream(aim_file, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.Write, 4); //二进制写入器 System.IO.BinaryWriter bWrite = new System.IO.BinaryWriter(fsAim); while (z >= 4096) { z = (int)bRead.Read(bytTemp, 0, bytTemp.Length);//读入字节数组,返回读取的字节数量,如果小于4096,则到了文件尾 bWrite.Write(bytTemp, 0, z);//从字节数组写入目标文件流 lngHad += z; string show = "From " + source_file + "\n" + "to " + aim_file; OnCopyFile(lngHad, lngCount);//触发事件 来控制主线程 这里是进度条和已完成复制文件字节显示 } _currentSize = lngHad; bWrite.Flush();//清理缓存区 bWrite.Close(); bRead.Close(); fsAim.Close(); fsSource.Close(); //修改文件时间以保持与源文件一致 System.IO.File.SetCreationTime(aim_file, System.IO.File.GetCreationTime(source_file)); System.IO.File.SetLastWriteTime(aim_file, System.IO.File.GetLastWriteTime(source_file)); System.IO.File.SetLastAccessTime(aim_file, System.IO.File.GetLastAccessTime(source_file)); //目标文件的属性与源文件一致 System.IO.File.SetAttributes(aim_file, System.IO.File.GetAttributes(source_file)); 呵呵,看到图片了.你把copy的代码贴出来看看吧.是不是循环出错了. //判断文件 private string FileCheck(string source_file, string aim_file) { long souLong = new System.IO.FileInfo(source_file).Length; if (System.IO.File.Exists(aim_file)) { long aimLong = new System.IO.FileInfo(aim_file).Length; DateTime SourceFileDateTime = System.IO.File.GetLastWriteTime(source_file); DateTime AimFileDateTime = System.IO.File.GetLastWriteTime(aim_file); if (souLong == aimLong) { if (SourceFileDateTime.CompareTo(AimFileDateTime) <= 0) { return "not copy"; } else { System.IO.File.SetAttributes(aim_file, FileAttributes.Normal); return aim_file; } } else { int leng = source_file.LastIndexOf("\\") + 1; string fileName = source_file.Substring(leng, source_file.Length - leng); int len = fileName.LastIndexOf('.'); string frontName = fileName.Substring(0, len); string extenName = fileName.Substring(len, fileName.Length - len); string strName = frontName; string sReturn; for (int i = 1; ; i++) { strName = frontName + "000" + i.ToString() + extenName; sReturn = aim_file.Replace(fileName, strName); long sLong, aLong; DateTime sDateTime, aDateTime; sLong = new System.IO.FileInfo(source_file).Length; if (System.IO.File.Exists(sReturn)) { aLong = new System.IO.FileInfo(sReturn).Length; sDateTime = System.IO.File.GetLastWriteTime(source_file); aDateTime = System.IO.File.GetLastWriteTime(sReturn); if (sLong == aLong) { if (sDateTime.CompareTo(aDateTime) <= 0) { return "not copy"; } else { System.IO.File.SetAttributes(sReturn, FileAttributes.Normal); return sReturn; } } } else { return sReturn; } } // return aim_file; } } else { return aim_file; } } }原文件 和目标文件对比 返回目标文件名称 如果你是为了练习这么写,就当我这贴不存在.如果是用在程序中,你只需简单写成: string source_file = "E:\\fox.jpg", aim_file = "E:\\f.jpg"; if (!File.Exists(source_file)) { return; } File.WriteAllBytes(aim_file, File.ReadAllBytes(source_file)); //修改文件时间以保持与源文件一致 System.IO.File.SetCreationTime(aim_file, System.IO.File.GetCreationTime(source_file)); System.IO.File.SetLastWriteTime(aim_file, System.IO.File.GetLastWriteTime(source_file)); System.IO.File.SetLastAccessTime(aim_file, System.IO.File.GetLastAccessTime(source_file)); //目标文件的属性与源文件一致 System.IO.File.SetAttributes(aim_file, System.IO.File.GetAttributes(source_file)); 呵呵 谢谢wdgphc 不能像你说的那样,我要拷贝文件夹下的所有文件,区别是什么文件然后归类 这样使用PictureImage 控件是否正确 正则表达式的问题 winform 套打方案 谁能给小弟一种或几种命名规范 sql查询语句,关于自动编号 删除文件问题?无法删除,但程序不报错!!! 谁能解决问题:100分都给你了,绝不食言 C# 抓包工具源代码 求高手。 怎么禁止在.aspx页面上用鼠标右键呀? 一个用于加密文件的方法。为什么速度这么慢?请大家帮我看看啊。谢了! 接口与对象 急,atagridview中如何checkbox的控件对象?
拷贝到目录后变成了
B-PNG_LI-26.png,
B-JPEG_M-19.jpeg
..B-PNG_LI-26.png.jpg,
...B-PNG_LI-26.png.jpg.jpg,
...B-PNG_LI-26.png.jpg.jpg.jpg,
....B-PNG_LI-26.png.jpg.jpg.jpg.jpg,"..B-PNG_LI-26.png.jpg,
...B-PNG_LI-26.png.jpg.jpg,
...B-PNG_LI-26.png.jpg.jpg.jpg,
....B-PNG_LI-26.png.jpg.jpg.jpg.jpg,"这些都是隐藏的 并且大小也要比
B-PNG_LI-26.png小一些
{
return;
} byte[] bytTemp = new byte[4096];//字节数组 //long lngHad = 0;
long lngHad = _currentSize;
long lngCount;
int z = 5000; //源文件流
System.IO.FileStream fsSource = new System.IO.FileStream(source_file, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read, 4);
//二进制读取器
System.IO.BinaryReader bRead = new System.IO.BinaryReader(fsSource);
//定位源文件流的头部
bRead.BaseStream.Seek(0, System.IO.SeekOrigin.Begin);
if (fsSource.Position > 0)
fsSource.Position = 0; lngCount = _totalSize;
//目标文件流
System.IO.FileStream fsAim = new System.IO.FileStream(aim_file, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.Write, 4); //二进制写入器
System.IO.BinaryWriter bWrite = new System.IO.BinaryWriter(fsAim); while (z >= 4096)
{
z = (int)bRead.Read(bytTemp, 0, bytTemp.Length);//读入字节数组,返回读取的字节数量,如果小于4096,则到了文件尾
bWrite.Write(bytTemp, 0, z);//从字节数组写入目标文件流 lngHad += z;
string show = "From " + source_file + "\n" + "to " + aim_file;
OnCopyFile(lngHad, lngCount);//触发事件 来控制主线程 这里是进度条和已完成复制文件字节显示
} _currentSize = lngHad; bWrite.Flush();//清理缓存区 bWrite.Close();
bRead.Close(); fsAim.Close();
fsSource.Close(); //修改文件时间以保持与源文件一致
System.IO.File.SetCreationTime(aim_file, System.IO.File.GetCreationTime(source_file));
System.IO.File.SetLastWriteTime(aim_file, System.IO.File.GetLastWriteTime(source_file));
System.IO.File.SetLastAccessTime(aim_file, System.IO.File.GetLastAccessTime(source_file));
//目标文件的属性与源文件一致
System.IO.File.SetAttributes(aim_file, System.IO.File.GetAttributes(source_file));
是不是循环出错了.
private string FileCheck(string source_file, string aim_file)
{
long souLong = new System.IO.FileInfo(source_file).Length;
if (System.IO.File.Exists(aim_file))
{
long aimLong = new System.IO.FileInfo(aim_file).Length;
DateTime SourceFileDateTime = System.IO.File.GetLastWriteTime(source_file);
DateTime AimFileDateTime = System.IO.File.GetLastWriteTime(aim_file);
if (souLong == aimLong)
{
if (SourceFileDateTime.CompareTo(AimFileDateTime) <= 0)
{
return "not copy";
}
else
{
System.IO.File.SetAttributes(aim_file, FileAttributes.Normal);
return aim_file;
}
}
else
{
int leng = source_file.LastIndexOf("\\") + 1;
string fileName = source_file.Substring(leng, source_file.Length - leng); int len = fileName.LastIndexOf('.');
string frontName = fileName.Substring(0, len);
string extenName = fileName.Substring(len, fileName.Length - len);
string strName = frontName;
string sReturn; for (int i = 1; ; i++)
{
strName = frontName + "000" + i.ToString() + extenName; sReturn = aim_file.Replace(fileName, strName); long sLong, aLong;
DateTime sDateTime, aDateTime; sLong = new System.IO.FileInfo(source_file).Length; if (System.IO.File.Exists(sReturn))
{
aLong = new System.IO.FileInfo(sReturn).Length;
sDateTime = System.IO.File.GetLastWriteTime(source_file);
aDateTime = System.IO.File.GetLastWriteTime(sReturn); if (sLong == aLong)
{
if (sDateTime.CompareTo(aDateTime) <= 0)
{
return "not copy";
}
else
{
System.IO.File.SetAttributes(sReturn, FileAttributes.Normal);
return sReturn;
}
}
}
else
{
return sReturn;
}
}
// return aim_file;
}
}
else
{
return aim_file;
}
}
}原文件 和目标文件对比 返回目标文件名称
如果是用在程序中,你只需简单写成:
string source_file = "E:\\fox.jpg", aim_file = "E:\\f.jpg";
if (!File.Exists(source_file))
{
return;
}
File.WriteAllBytes(aim_file, File.ReadAllBytes(source_file));
//修改文件时间以保持与源文件一致
System.IO.File.SetCreationTime(aim_file, System.IO.File.GetCreationTime(source_file));
System.IO.File.SetLastWriteTime(aim_file, System.IO.File.GetLastWriteTime(source_file));
System.IO.File.SetLastAccessTime(aim_file, System.IO.File.GetLastAccessTime(source_file));
//目标文件的属性与源文件一致
System.IO.File.SetAttributes(aim_file, System.IO.File.GetAttributes(source_file));