C# 在C中循环文件文件夹最简单的方法是什么?

C# 在C中循环文件文件夹最简单的方法是什么?,c#,parsing,file-io,filesystems,C#,Parsing,File Io,Filesystems,我试图编写一个程序,使用包含相关文件路径的配置文件导航本地文件系统。我的问题是:在执行文件I/O(从桌面应用程序到服务器,再到服务器,以及在C中的文件系统导航)时,最佳做法是什么 我知道如何使用谷歌,我已经找到了几种解决方案,但我想知道各种功能中哪一种最健壮、最灵活。另外,如果任何人有任何关于C文件I/O异常处理的提示,这也会非常有用。您不需要单独的库,请使用System.IO命名空间中的类,如file、FileInfo、Directory、DirectoryInfo。一个简单的例子: var

我试图编写一个程序,使用包含相关文件路径的配置文件导航本地文件系统。我的问题是:在执行文件I/O(从桌面应用程序到服务器,再到服务器,以及在C中的文件系统导航)时,最佳做法是什么


我知道如何使用谷歌,我已经找到了几种解决方案,但我想知道各种功能中哪一种最健壮、最灵活。另外,如果任何人有任何关于C文件I/O异常处理的提示,这也会非常有用。

您不需要单独的库,请使用System.IO命名空间中的类,如file、FileInfo、Directory、DirectoryInfo。一个简单的例子:

var d = new DirectoryInfo(@"c:\");
foreach(FileInfo fi in d.GetFiles())
    Console.WriteLine(fi.Name);

你在谈论什么样的图书馆

我会坚持使用System.IO.Directory等等。它有你需要的一切

比如:

foreach (var file in System.IO.Directory.GetFiles(@"C:\Yourpath"))
{
    // Do ya thang.
}

System.IO命名空间中可以使用各种类,包括File、FileInfo、Directory和DirectoryInfo

至于实践。。。与任何IO一样,确保关闭所有打开的流。您可能还需要使用一次性物品,因此请查看using关键字。

System.IO是您所需要的全部:

关于异常处理。除非我们期待着一个例外,否则我们永远不会抓住它。真正意外的异常应该不被处理。[看起来有罪]好的,唯一的例外是在最高级别,并且仅用于报告目的,例如

// assuming console program, but every application Console, WinForm,
// Wpf, WindowsService, WebService, WcfService has a similar entry point
class Program
{
    // assume log4net logging here, but could as easily be
    // Console.WriteLine, or hand rolled logger
    private static readonly ILog _log = LogManager.GetLogger (typeof (Program));

    static void Main (string[] args)
    {
        AppDomain.CurrentDomain.UnhandledException += 
            CurrentDomain_UnhandledException;
    }

    private static void CurrentDomain_UnhandledException (
        object sender, 
        UnhandledExceptionEventArgs e)
    {
        _log.
            Fatal (
            string.Format (
            "Unhandled exception caught by " +
            "'CurrentDomain_UnhandledException'. Terminating program.", 
            e.ExceptionObject);
    }

}
如果您希望出现异常,则可以接受以下情况之一

// example of first option. this applies ONLY when there is a
// well-defined negative path or recovery scenario
public void SomeFunction ()
{
    try
    {
        string allText = System.IO.File.ReadAllText ();
    }

    // catch ONLY those exceptions you expect
    catch (System.ArgumentException e)
    {
        // ALWAYS log an error, expected or otherwise.
        _log.Warn ("Argument exception in SomeFunction", e);

        // if the use-case\control flow is recoverable, invoke
        // recovery logic, preferably out of try-catch scope
    }
}


很抱歉,还是C的新手。我的意思是我发现了多种技术,但它们的方法似乎都包含在System.IO.File类中。@badpanda:您选择的类/方法取决于您尝试执行的具体操作。你能提供更多的细节吗?当然。我需要读入稍后发送到web服务的文件。这些文件很可能是infopath文件。如果相关,我还需要文件属性中的一些信息。有关访问属性的示例,请参阅。
// example of second option. this applies ONLY when there is no
// well defined negative path and we require additional information
// on failure
public void SomeFunction ()
{
    try
    {
        string allText = System.IO.File.ReadAllText ();
    }

    // catch ONLY those exceptions you expect
    catch (System.ArgumentException innerException)
    {
         // do whatever you need to do to identify this
         // problem area and provide additional context
         // like parameters or what have you. ALWAYS
         // provide inner exception
         throw new SomeCustomException (
             "some new message with extra info.",
             maybeSomeAdditionalContext,
             innerException);

         // no requirement to log, assume caller will
         // handle appropriately
    }
}