File's custom attributes

D

Duncan McNutt

Gets the FileAttributes of the file on the path.

[Visual Basic]
Public Shared Function GetAttributes( _
ByVal path As String _
) As FileAttributes
[C#]
public static FileAttributes GetAttributes(
string path
);
[C++]
public: static FileAttributes GetAttributes(
String* path
);
[JScript]
public static function GetAttributes(
path : String
) : FileAttributes;
Parameters
path
The path to the file.
Return Value
The FileAttributes of the file on the path, or -1 if the path or file is not
found.

Exceptions
Exception Type Condition
ArgumentException path is empty, contains only white spaces, or
contains invalid characters.
PathTooLongException The specified path, file name, or both exceed the
system-defined maximum length. For example, on Windows-based platforms,
paths must be less than 248 characters, and file names must be less than 260
characters.
NotSupportedException path is in an invalid format.
DirectoryNotFoundException The specified path is invalid, such as
being on an unmapped drive.

Remarks
The path parameter is permitted to specify relative or absolute path
information. Relative path information is interpreted as relative to the
current working directory. To obtain the current working directory, see
GetCurrentDirectory.

For an example of using this method, see the Example section below. The
following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File
File.AppendText

FileInfo.AppendText

Rename or move a file. File.Move
FileInfo.MoveTo

Read from a binary file. Reading and Writing to a Newly Created Data
File
Write to a binary file. Reading and Writing to a Newly Created Data
File
Set file attributes. SetAttributes

Example
[Visual Basic, C#, C++] The following example demonstrates the GetAttributes
and SetAttributes methods by applying the Archive and Hidden attributes to a
file.

[Visual Basic]
Imports System
Imports System.IO
Imports System.Text

Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
' Delete the file if it exists.
If File.Exists(path) = False Then
File.Create(path)
End If

If (File.GetAttributes(path) And FileAttributes.Hidden) =
FileAttributes.Hidden Then
' 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, File.GetAttributes(path) Or
FileAttributes.Hidden)
Console.WriteLine("The {0} file is now hidden.", path)
End If
End Sub
End Class
[C#]
using System;
using System.IO;
using System.Text;

class Test
{
public static void 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, File.GetAttributes(path) |
FileAttributes.Hidden);
Console.WriteLine("The {0} file is now hidden.", path);
}
}
}
[C++]
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;

void main() {
String* path = S"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(S"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(S"The {0} file is now hidden.", path);
}
}
 
S

StGo

How can i read/write file's custom attributs(like subject,author...) in
C#???

Thanks :))
 
S

StGo 13

Sank's for answer!

But I'm mean to non standart attributs like subject,comments and
possibility adding custom attributs
like "custom_version=1.6".

If you can,please help me:)
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top