So here in my code, I am editing the word documents from a folder, and copied those files over to a temp folder and I was just wondering how I could access those files in the temp folder to pursue further actions. Here is my code:
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();
}
}
}
I think the thing that you need is DirectoryInfo class, according to your code you can open needed directory like that
var directory = new DirectoryInfo(destinationFolder);
And then, I suppose, get files enumerable collection like that
var files = directory.EnumerateFiles();
Theres reference for further reading on MSDN
Also I can suggest few improvements to your code!
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();
}
}