Передача пути в DirectoryInfo из XML - C#
Формулировка задачи:
Коллеги, добрый день. Пытаюсь передать в качестве параметра пути в DirectoryInfo значение из xml файла. При запуске компилятора студия выдает ошибку Illegal characters in path. В чем проблема?
Код приложения:
Код xml:
Как можно исправит?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;
using System.Collections;
namespace WindowsFormsApplication1
{
class Parser
{
static string source = "source.xml";
//Insert directory from xml
public string GetDirectory()
{
var doc = XDocument.Load(source);
var path = doc.Descendants("filesource").First().Value;
return path;
}
public int GetPeriod()
{
var doc = XDocument.Load(source);
var per = doc.Descendants("period").First().Value;
int period = Convert.ToInt32(per);
return period;
}
//Insert message text from xml
public string GetMessage()
{
var doc = XDocument.Load(source);
var msg = doc.Descendants("message").First().Value;
return msg;
}
}
//Checking files
class FileChecker
{
static Parser parser = new Parser();
ArrayList actualFiles = new ArrayList();
int period = parser.GetPeriod();
string path = parser.GetDirectory();
public ArrayList SearchFiles()
{
DirectoryInfo dir = new DirectoryInfo(path); //ОШИБКА ТУТ
foreach (FileInfo item in dir.GetFiles())
{
DateTime fileCreation = item.CreationTime;
if (fileCreation >= (DateTime.Now.AddMinutes(-period)))
actualFiles.Add(item);
}
return actualFiles;
}
}
class LogFile
{
public DateTime thisDate = new DateTime();
public ArrayList fileList = new FileChecker().SearchFiles();
public void Logging()
{
string thisDateStr = thisDate.ToString("dd-M-yyyy");
thisDateStr += ".txt";
string logPathAndName = "/logs/" + thisDateStr;
try
{
//Open the File
StreamWriter sw = new StreamWriter(thisDateStr, true);
foreach (FileInfo file in fileList)
{
sw.WriteLine(file.Name + " - " + file.CreationTime + " - сообщение выведено на экран");
}
//close the file
sw.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
LogFile logs = new LogFile();
logs.Logging();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}<?xml version='1.0' encoding="UTF-8" ?> <source> <configuration> <filesource> \\IGUNCHEN\\data </filesource> <period> 80 </period> <message> Вам пришел новый документ </message> </configuration> </source>
Решение задачи: «Передача пути в DirectoryInfo из XML»
textual
Листинг программы
<filesource>\IGUNCHEN\data</filesource>