![](/img/trans.png)
[英]Azure Data Lake Gen2 - How do I move files from folder to another folder using C#
[英]How can I move into another folder to access other files in C#
所以在我的代码中,我正在编辑文件夹中的 word 文档,并将这些文件复制到临时文件夹中,我只是想知道如何访问临时文件夹中的这些文件以执行进一步的操作。 这是我的代码:
private static void editFieldsTest(string filename)
{
if (filename.StartsWith("q") | filename.Contains("cover"))
{
Console.WriteLine("\nMoving question files to temp directory\n");
var destinationFolder = @"temp\";
var tmp_filename = $"{destinationFolder}{ Path.GetFileName(filename)}";
try
{
File.Copy(filename, tmp_filename, true);
}
catch (IOException iox)
{
Console.WriteLine(iox.Message);
}
int fileLen = filename.Length;
Console.WriteLine("editing question files...\n");
string typeOfQuestion = filename.Substring(1, fileLen - 14);
string timeGiven = filename.Substring(3, fileLen - 14);
string marks = filename.Substring(5, fileLen - 14);
string learningOutcome = filename.Substring(7, fileLen - 14);
//string fifthIndex = filename.Substring(9, fileLen - 9);
var valuesToFill = new Content(
new FieldContent("qnumber", typeOfQuestion),
new FieldContent("qmark", marks),
new FieldContent("qtime", timeGiven),
new FieldContent("learningOutcome", learningOutcome));
using (var outputDocument = new TemplateProcessor(tmp_filename)
.SetRemoveContentControls(true))
{
outputDocument.FillContent(valuesToFill);
outputDocument.SaveChanges();
}
}
}
我认为您需要的是 DirectoryInfo 类,根据您的代码,您可以像这样打开所需的目录
var directory = new DirectoryInfo(destinationFolder);
然后,我想,像这样获取文件可枚举集合
var files = directory.EnumerateFiles();
在MSDN 上有进一步阅读的参考
我也可以建议对您的代码进行一些改进!
private static void editFieldsTest(string filename)
{
//By returning early you're reducing nesting and making your code more readable.
//Fail fast!
if (!(filename.StartsWith("q") | filename.Contains("cover")))
return;
Console.WriteLine("\nMoving question files to temp directory\n");
//Make things const and move them our from your method
//But only if you really think that they won't change
//I suggest moving that const to method signature to improve extensibility
const string destinationFolder = @"temp\";
var tmpFilename = $"{destinationFolder}{ Path.GetFileName(filename)}";
try
{
File.Copy(filename, tmpFilename, true);
}
catch (IOException iox)
{
Console.WriteLine(iox.Message);
}
//You can use var to redeem yourself from writing whole type every time
var fileLen = filename.Length;
Console.WriteLine("editing question files...\n");
var typeOfQuestion = filename.Substring(1, fileLen - 14);
var timeGiven = filename.Substring(3, fileLen - 14);
var marks = filename.Substring(5, fileLen - 14);
var learningOutcome = filename.Substring(7, fileLen - 14);
//string fifthIndex = filename.Substring(9, fileLen - 9);
var valuesToFill = new Content(
new FieldContent("qnumber", typeOfQuestion),
new FieldContent("qmark", marks),
new FieldContent("qtime", timeGiven),
new FieldContent("learningOutcome", learningOutcome));
using (var outputDocument = new TemplateProcessor(tmpFilename)
.SetRemoveContentControls(true))
{
outputDocument.FillContent(valuesToFill);
outputDocument.SaveChanges();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.