Visual C++ .NET/File Directory/DirectoryInfo

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Does a DirectoryInfo Exist

 
#include "stdafx.h"
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main(array<System::String ^> ^args)
{
    DirectoryInfo ^dir = gcnew DirectoryInfo("c:\\");
    if (!dir->Exists)
    {
        Console::WriteLine("Directory Not Found");
        return -1;
    }

    return 0;
}


Get Directory Attributes

 
#include "stdafx.h"
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main(array<System::String ^> ^args)
{
    DirectoryInfo ^dir = gcnew DirectoryInfo("c:\\");
    Console::WriteLine("Attributes: {0}", 
        dir->Attributes);
    return 0;
}


Get Directory creation time

 
#include "stdafx.h"
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main(array<System::String ^> ^args)
{
    DirectoryInfo ^dir = gcnew DirectoryInfo("c:\\");
    Console::WriteLine("Created:    {0} {1}", 
        dir->CreationTime.ToShortDateString(),
        dir->CreationTime.ToLongTimeString());
    return 0;
}


Get Directory last access time

 
#include "stdafx.h"
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main(array<System::String ^> ^args)
{
    DirectoryInfo ^dir = gcnew DirectoryInfo("c:\\");
    Console::WriteLine("Accessed:   {0} {1}", 
        dir->LastAccessTime.ToShortDateString(),
        dir->LastAccessTime.ToLongTimeString());
    return 0;
}


Get Directory last write time

 
#include "stdafx.h"
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main(array<System::String ^> ^args)
{
    DirectoryInfo ^dir = gcnew DirectoryInfo("c:\\");
    Console::WriteLine("Updated:    {0} {1}", 
        dir->LastWriteTime.ToShortDateString(),
        dir->LastWriteTime.ToLongTimeString());
    return 0;
}


Get Files under a directory

 
#include "stdafx.h"
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main(array<System::String ^> ^args)
{
    DirectoryInfo ^dir = gcnew DirectoryInfo("c:\\");
    array<FileInfo^>^ files = dir->GetFiles();
    if (files->Length == 0)
        Console::WriteLine("\tNone."); 
    else
    {
        for each (FileInfo^ finfo in files)
        {
            Console::WriteLine("\t{0}", finfo->Name); 
        }
    }
    return 0;
}


Get Sub-Directories

 
#include "stdafx.h"
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main(array<System::String ^> ^args)
{
    DirectoryInfo ^dir = gcnew DirectoryInfo("c:\\");
    array<DirectoryInfo^>^ subDirs = dir->GetDirectories();
    if (subDirs->Length == 0)
        Console::WriteLine("\tNone."); 
    else
    {
        for each (DirectoryInfo^ dinfo in subDirs)
        {
            Console::WriteLine("\t{0}", dinfo->Name); 
        }
    }
    return 0;
}


Get the Directory full name

 
#include "stdafx.h"
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main(array<System::String ^> ^args)
{
    DirectoryInfo ^dir = gcnew DirectoryInfo("c:\\");
    Console::WriteLine("Name:       {0}", dir->FullName);
    return 0;
}