Now you must take care while development and divide the task into logical chunks.
First of all you should develop a method that takes care of just getting the codes.
static IEnumerable<string> GetCodes(string filename)
{
var xml = File.ReadAllText(filename);
return Regex
.Matches(xml, @"P\d-\d-\d-\d-\d")
.Select(x => x.Value)
.Distinct()
.ToList();
}
The other function is called with the path of your XML files
static void AnalyzeFiles(string path)
{
var filenames = Directory.GetFiles(path, "*.xml");
foreach (var filename in filenames)
{
var codes = GetCodes(filename);
var destinationFilename = Path.Combine(path, $"{Path.GetFileNameWithoutExtension(filename)}_result.txt");
File.WriteAllLines(destinationFilename, codes);
}
}
It can be called like this:
AnalyzeFiles(Path.GetDirectoryName(@"c:\temp\test"));
Now you should read the Microsoft documentation in order to understand things like "Path.Combine", "Path.GetFileNameWithoutExtension" etc.