问题
I'm having a problem when creating a DirectoryInfo, The directory I give when creating the DirectoryInfo is this:
"S:\PUBLICO\ESTUDIOS DIGITALIZADOS\PREVENTIVA\ELECTROS\2020\01-ENERO\08\"
And when checking if DirectoryInfo exists it's false, even though it exists. This is a problem because it throws an exception even when using di.GetFiles().
Also, when accessing folders from 2019 instead of 2020 it works perfectly, and uses the exact same code
This is the code that matters:
strDirectorioBase = strDirectorioECG + "\\" + Anio + "\\" + Mes + "-" + NombreMes + "\\" + Dia + "\\";
try
{
DirectoryInfo di = new DirectoryInfo(strDirectorioBase);
/*FileInfo[] a = di.GetFiles(strFiltro, SearchOption.AllDirectories);*/
MessageBox.Show(di.Exists.ToString());
if (di.Exists)
{
MessageBox.Show("Existe");
}
foreach (var fi in di.GetFiles(strFiltro, SearchOption.AllDirectories))
{
回答1:
If you do not have access to the folder GetFiles will not continue past exception. You need to use recursive code like below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace SAveDirectoriesXml
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
const string FOLDER = @"c:\temp";
static XmlWriter writer = null;
static void Main(string[] args)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
writer = XmlWriter.Create(FILENAME, settings);
writer.WriteStartDocument(true);
DirectoryInfo info = new DirectoryInfo(FOLDER);
WriteTree(info);
writer.WriteEndDocument();
writer.Flush();
writer.Close();
Console.WriteLine("Enter Return");
Console.ReadLine();
}
static long WriteTree(DirectoryInfo info)
{
long size = 0;
writer.WriteStartElement("Folder");
try
{
writer.WriteAttributeString("name", info.Name);
writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
writer.WriteAttributeString("date", info.LastWriteTime.ToString());
foreach (DirectoryInfo childInfo in info.GetDirectories())
{
size += WriteTree(childInfo);
}
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error", errorMsg);
}
FileInfo[] fileInfo = null;
try
{
fileInfo = info.GetFiles();
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error",errorMsg);
}
if (fileInfo != null)
{
foreach (FileInfo finfo in fileInfo)
{
try
{
writer.WriteStartElement("File");
writer.WriteAttributeString("name", finfo.Name);
writer.WriteAttributeString("size", finfo.Length.ToString());
writer.WriteAttributeString("date", info.LastWriteTime.ToString());
writer.WriteEndElement();
size += finfo.Length;
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error", errorMsg);
}
}
}
writer.WriteElementString("size", size.ToString());
writer.WriteEndElement();
return size;
}
}
}
来源:https://stackoverflow.com/questions/60281076/using-directoryinfo-exists-is-false-even-if-the-directory-exists