Exception/StackTrace - Could not find a part of the path 'c:\xxy\newfile' This means your code is trying to access or create a file in a folder that doesn’t exists. In this case, the folder c:\xxy\ is missing.
Problem:
The actual issue is in your method:
ScanFolder.Program.DirSearch(String directoryPath, String par_OutFile)
Where you are passing c:\xxy\newfile as par_OutFile to StreamWriter.
Since the folder c:\xxy\ doesn’t exists, .Net cannot create newfile.
Fix:
Before writing the file, make sure the directory exists:
String outFile = @”c:\xxy\newfile”;
//Ensure directory exists
String directory = Path.GetDirectoryName(outFile);
If(!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
//Now safe to create StreamWriter
Using(StreamWriter writer = new SreamWriter(outFile, true))
{
writer.WriteLine(“Test content”);
}