我必须将图像从“SourceFolder / Images”传输到 asp.net c#中的“DestinationFolder / Photos”。应将源文件夹中的所有图像复制到目标,并使用新生成的名称重命名原始图像名称。例如,如果源文件夹中的文件是mountain.jpg,并且在将此图像名称复制到目标文件夹时,则需要将其重命名为Current DateTime,后跟下划线和原始文件名(2016-05-20_mountain.jpg)。
我的代码如下:
string sourcePath = Server.MapPath("~/SourceFolder/Images");
string targetPath = Server.MapPath("~/DestinationFolder/Photos");
foreach (var srcPath in Directory.GetFiles(sourcePath))
{
File.Copy(srcPath, srcPath.Replace(sourcePath, targetPath), true);
}
上面的代码成功地将所有文件复制到与原始名称相同的目标路径,但是我想在将文件名传输到目标位置时将每个文件重命名为不同的名称。
答案 0 :(得分:3)
我已将代码更改为:
string sourcePath = Server.MapPath("~/SourceFolder/Images");
string targetPath = Server.MapPath("~/DestinationFolder/Photos");
foreach (var srcPath in Directory.GetFiles(sourcePath))
{
FileInfo fileInfo = new FileInfo(srcPath);
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + fileInfo.Name;
File.Copy(srcPath, srcPath.Replace(sourcePath, targetPath).Replace(fileInfo.Name, fileName), true);
}
或者您也可以使用:
string sourcePath = Server.MapPath("~/SourceFolder/Images");
string targetPath = Server.MapPath("~/DestinationFolder/Photos");
foreach (var srcPath in Directory.GetFiles(sourcePath))
{
FileInfo fileInfo = new FileInfo(srcPath);
string fileName = string.Format("{0}_{1}", DateTime.Now.ToString("yyyyMMddHHmmss"), fileInfo.Name);
File.Copy(srcPath, string.Format("{0}/{1}", targetPath, fileName), true);
}
您可以根据需要更改DateTime格式。
答案 1 :(得分:1)
static void CopytoDestination(string sourcePath,string sourcePath)
{
string fileName = "test.txt";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// 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 a file to another location and
// overwrite the destination file if it already exists.
System.IO.File.Copy(sourceFile, destFile, true);
// 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.
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
}
else
{
Console.WriteLine("Source path does not exist!");
}
// Keep console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
答案 2 :(得分:0)
protected void btnTransferFiles_Click(object sender, EventArgs e)
{
string sourcePath = Server.MapPath("~/SourceFolder/Images");
string targetPath = Server.MapPath("~/DestinationFolder/Photos");
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
string fileName = string.Empty;
string destFile = string.Empty;
// 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.
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + fileName);
System.IO.File.Copy(s, destFile, true);
}
}
}