By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

File's custom attributes

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

Thanks :))
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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(pa th) |
FileAttributes::Hidden));
Console::WriteLine(S"The {0} file is now hidden.", path);
}
}
--

Duncan McNutt
Microsoft Product Deactivation Team
--
"StGo" <go******@ness-isi.com> wrote in message
news:eQ**************@TK2MSFTNGP09.phx.gbl...
How can i read/write file's custom attributs(like subject,author...) in
C#???

Thanks :))

Nov 15 '05 #2

P: n/a
Hi, StGo:
If you mean the summary(auther, title, subject) properties of files
on NTFS or ole ducments, you may want to check out Dsofile activx control
provided by Microsoft. You need to download it from the following link and
import it into your project.

http://support.microsoft.com/default...NoWebContent=1

Hope it helps
Qiu

"StGo" <go******@ness-isi.com> wrote in message
news:eQ**************@TK2MSFTNGP09.phx.gbl...
How can i read/write file's custom attributs(like subject,author...) in
C#???

Thanks :))

Nov 15 '05 #3

P: n/a


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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.