问题描述
请帮助我如何在一个文件夹中创建一个子文件夹(带日期),我们需要将文件从文件夹移动到子文件夹。写下面的代码来上传文件但我是无法创建新的日期文件夹并将文件移动到该文件夹。
我尝试过:
please help me how can we create a subfolder(with date) in a folder where we need to move the files from folder to subfolder.written below code to upload files but i am unable to create a new date folder and move files to that folder.
What I have tried:
Move upload
if(!Directory.Exists(_SelectedPath+\\"UPLOAD"))
{
Directory.CreateDirectory(_SelectedPath "UPLOAD");
}
// var Folder = Directory.CreateDirectory(_SelectedPath+"UPLOAD");
// string fileName = SourceFileName;
// String extension = Path.GetExtension(file1.Extension);
string sourcePath = @_SelectedPath;
string targetPath = @_SelectedPath+"UPLOAD" +@"\"+ FileName+ "_" + ISO_Date();
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(targetPath, FileName);
string destFile = System.IO.Path.Combine(sourceFile, FileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
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, targetPath, true);
}
}
推荐答案
是的我已经完成了这样的c:/ folder1 / subfolder1 / 11-05-2018我已经做到文件夹路径我需要再创建一个日期目录,每当我上传文件时需要创建当前日期目录。你能建议吗
yes i have done like this c:/folder1/subfolder1/11-05-2018 i have done till folder path i need to create one more directory with date when i upload file every time i need to create current date directory. can you suggest
你的意思是:
You mean:
string dirName = Path.Combine(@"C:\folder1\subfolder1", DateTime.Now.ToString("dd-MM-yyyy"));
Directory.CreateDirectory(dirName);
但如果您正在谈论上传,那么您可能无法直接在C:驱动器上创建文件夹,因为您的网站几乎肯定没有;没有必要权限。您应该使用Server.MapPath将网站相对路径转换为文件系统路径并将文件存储在您的网站文件夹结构下。
But if you are talking upload, then you probably can't create folders on the C: drive directly as your website almost certainly doesn;t have the required permissions. You should probably use Server.MapPath to convert a web-site relative path to a file system path and store your files under your website folder structure.
String Todaysdate = DateTime.Now.ToString("dd-MMM-yyyy");
string datefolder = Path.Combine(subFolder,Todaysdate);
if(!Directory.Exists(datefolder))
{
Directory.CreateDirectory(datefolder);
}
string sourcePath = firstFolder;
string targetPath = datefolder; }
string sourcePath = firstFolder;
string targetPath = datefolder;
这篇关于我们如何在子文件夹中创建日期文件夹并将子文件夹的文件移动到日期文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!