La classe system.io

Messaggioda carlo23 » 09/06/2006, 17:30

Ciao,

in C++ mi servirebbe sapere come posso gestire file e directory, conosco già come scrivere o leggere un file con fstream e iostream, mi servirebbe poter elencare il contenuo di una directory e poter leggere le proprietà di un file quali data di creazione e ultima modifica... cercando su internet ho trovato che devo usare la classe system.io

Qualcuno la conosce e potrebbe spiegarmela, magari facendo un piccolo esempio di programma consol per sfogliare una directory? :D

Grazie, ciao :D
carlo23
Senior Member
Senior Member
 
Messaggio: 1095 di 1683
Iscritto il: 01/11/2005, 19:38

Re: La classe system.io

Messaggioda anonymous_be1147 » 10/06/2006, 16:52

A quanto pare la classe System.IO non appartiene alla libreria standard C++, ma è roba Microsoft .NET.

Comunque, se ti può servire, ecco un esempio (preso dall'help in linea di Visual C++ 2005 Express Edition) di utilizzo per verificare/impostare l'attributo nascosto di un certo file:
Codice:
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   
   // Delete the file if it exists.
   if (  !File::Exists( path ) )
   {
      File::Create( path );
   }

   if ( (File::GetAttributes( path ) & FileAttributes::Hidden) == FileAttributes::Hidden )
   {
     
      // Show the file.
      File::SetAttributes( path, FileAttributes::Archive );
      Console::WriteLine( "The {0} file is no longer hidden.", path );
   }
   else
   {
     
      // Hide the file.
      File::SetAttributes( path, static_cast<FileAttributes>(File::GetAttributes( path ) | FileAttributes::Hidden) );
      Console::WriteLine( "The {0} file is now hidden.", path );
   }
}


Altro esempio che utilizza la classe FileInfo per conoscere nomi e dimensione dei file in una directory:
Codice:
// The following example displays the names and sizes
// of the files in the specified directory.
using namespace System;
using namespace System::IO;
int main()
{
   
   // Make a reference to a directory.
   DirectoryInfo^ di = gcnew DirectoryInfo( "c:\\" );
   
   // Get a reference to each file in that directory.
   array<FileInfo^>^fiArr = di->GetFiles();
   
   // Display the names and sizes of the files.
   Console::WriteLine( "The directory {0} contains the following files:", di->Name );
   System::Collections::IEnumerator^ myEnum = fiArr->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      FileInfo^ f = safe_cast<FileInfo^>(myEnum->Current);
      Console::WriteLine( "The size of {0} is {1} bytes.", f->Name, f->Length );
   }
}
anonymous_be1147
Cannot live without
Cannot live without
 
Messaggio: 78 di 3226
Iscritto il: 02/03/2006, 20:20

Re: La classe system.io

Messaggioda carlo23 » 10/06/2006, 21:33

anonymous_be1147 ha scritto:A quanto pare ....


Grazie, appena ho il compilatore sottomano gli provo e ti faccio sapere :D
carlo23
Senior Member
Senior Member
 
Messaggio: 1103 di 1683
Iscritto il: 01/11/2005, 19:38


Torna a Informatica

Chi c’è in linea

Visitano il forum: Nessuno e 1 ospite