Chilkat C# FileAccess Class Reference
FileAccess
API for reading and writing files, creating and deleting directories, deleting directory trees, splitting and re-joining large files, etc. This is a freeware class/library. The reason for its existence is that in some programming languages, file I/O API's are limited or difficult to understand/use. This API provides an identical FILE I/O API across all programming languages supported by Chilkat.
Object Creation
(C#)
Chilkat.FileAccess obj = new Chilkat.FileAccess();
(VB.NET)
Dim obj As New Chilkat.FileAccess()
Properties
public string CurrentDir {get; }
The current working directory of the calling process.
public bool EndOfFile {get; }
Returns true if the current open file is at the end-of-file.
public int FileOpenError {get; }
This property is set by the following methods: FileOpen, OpenForRead, OpenForWrite, OpenForReadWrite, and OpenForAppend. It provides an error code indicating the failure reason. Possible values are:
- Success (No error)
- Access denied.
- File not found.
- General (non-specific) open error.
- File aleady exists.
- Path refers to a directory and the access requested involves writing.
- Too many symbolic links were encountered in resolving path.
- The process already has the maximum number of files open.
- Pathname is too long.
- The system limit on the total number of open files has been reached.
- Pathname refers to a device special file and no corresponding device exists.
- Insufficient kernel memory was available.
- Pathname was to be created but the device containing pathname has no room for the new file.
- A component used as a directory in pathname is not, in fact, a directory.
- Pathname refers to a regular file, too large to be opened (this would be a limitation of the underlying operating system, not a limitation imposed by Chilkat).
- Pathname refers to a file on a read-only filesystem and write access was requested.
public string FileOpenErrorMsg {get; }
The error message text associated with the FileOpenError code.
public string LastErrorHtml {get; }
Error information in HTML format for the last method called.
public string LastErrorText {get; }
Error information in plain-text format for the last method called.
public string LastErrorXml {get; }
Error information in XML format for the last method called.
Methods
public bool AppendAnsi(string text);
Appends a string using the ANSI character encoding to the currently open file. Returns true for success, false for failure.
public bool AppendText(string str, string charset);
Appends a string using the character encoding specified by str to the currently open file. Returns true for success, false for failure.
public bool AppendUnicodeBOM();
Appends the 2-byte Unicode BOM (little endian) to the currently open file. Returns true for success, false for failure.
public bool AppendUtf8BOM();
Appends the 3-byte utf-8 BOM to the currently open file. Returns true for success, false for failure.
public bool DirAutoCreate(string dirPath);
Creates all directories necessary such that the entire dirPath exists. Returns true for success, false for failure.
public bool DirCreate(string dirPath);
Creates a new directory specified by dirPath. Returns true for success, false for failure.
public bool DirDelete(string dirPath);
Deletes the directory specified by dirPath. Returns true for success, false for failure.
public bool DirEnsureExists(string filePath);
Same as DirAutoCreate, except the argument is a file path (the last part of the path is a filename and not a directory). Creates all missing directories such that filePath may be created. Returns true for success, false for failure.
public void FileClose();
Closes the currently open file.
public bool FileCopy(string existingFilepath, string newFilepath, bool failIfExists);
Copys existingFilepath to newFilepath. If failIfExists is true and newFilepath already exists, then an error is returned. Returns true for success, false for failure.
public bool FileDelete(string filePath);
Deletes the file specified by filePath. Returns true for success, false for failure.
public bool FileExists(string filePath);
Returns true if filePath exists, otherwise returns false.
public bool FileOpen(string filePath, unsigned int accessMode, unsigned int shareMode, unsigned int createDisposition, unsigned int attributes);
This method should only be called on Windows operating systems. It's arguments are similar to the Windows Platform SDK function named CreateFile. For Linux, MAC OS X, and other operating system, use the OpenForRead, OpenForWrite, OpenForReadWrite, and OpenForAppend methods.
Opens a file for reading or writing. The arguments mirror the Windows CreateFile function:
Access Modes:
GENERIC_READ (0x80000000)
GENERIC_WRITE (0x40000000)
Share Modes:
FILE_SHARE_READ(0x00000001)
FILE_SHARE_WRITE(0x00000002)
Create Dispositions
CREATE_NEW 1
CREATE_ALWAYS 2
OPEN_EXISTING 3
OPEN_ALWAYS 4
TRUNCATE_EXISTING 5
// Attributes:
FILE_ATTRIBUTE_READONLY 0x00000001
FILE_ATTRIBUTE_HIDDEN 0x00000002
FILE_ATTRIBUTE_SYSTEM 0x00000004
FILE_ATTRIBUTE_DIRECTORY 0x00000010
FILE_ATTRIBUTE_ARCHIVE 0x00000020
FILE_ATTRIBUTE_NORMAL 0x00000080
FILE_ATTRIBUTE_TEMPORARY 0x00000100
Returns true for success, false for failure.
public byte[] FileRead(int maxNumBytes);
Reads bytes from the currently open file. maxNumBytes specifies the maximum number of bytes to read. Returns an empty byte array on error. Returns an empty byte array on failure
public bool FileRename(string existingFilepath, string newFilepath);
Renames a file from existingFilepath to newFilepath. Returns true for success, false for failure.
public bool FileSeek(int offset, int origin);
Sets the file pointer for the currently open file. The offset is an offset in bytes from the origin. The origin can be one of the following:
0 = Offset is from beginning of file.
1 = Offset is from current position of file pointer.
2 = Offset is from the end-of-file (offset may be negative).
Returns true for success, false for failure.
public int FileSize(string filePath);
Returns the size, in bytes, of a file. Returns -1 for failure.
public bool FileWrite(byte[] data);
Writes bytes to the currently open file. Returns true for success, false for failure.
public string GetTempFilename(string dirName, string prefix);
To be documented soon. Returns a null on failure
public bool OpenForAppend(string filePath);
Opens a file for appending. If filePath did not already exists, it is created. When an existing file is opened with this method, the contents will not be overwritten and the file pointer is positioned at the end of the file.
If the open/create failed, then error information will be available in the FileOpenError and FileOpenErrorMsg properties. Returns true for success, false for failure.
public bool OpenForRead(string filePath);
Opens a file for reading. The file may contain any type of data (binary or text) and it must already exist. If the open failed, then error information will be available in the FileOpenError and FileOpenErrorMsg properties. Returns true for success, false for failure.
public bool OpenForReadWrite(string filePath);
Opens a file for reading/writing. If filePath did not already exists, it is created. When an existing file is opened with this method, the contents will not be overwritten, but the file pointer is positioned at the beginning of the file.
If the open/create failed, then error information will be available in the FileOpenError and FileOpenErrorMsg properties. Returns true for success, false for failure.
public bool OpenForWrite(string filePath);
Opens a file for writing. If filePath did not already exists, it is created. When an existing file is opened with this method, the contents will be overwritten. (For example, calling OpenForWrite on an existing file and then immediately closing the file will result in an empty file.) If the open/create failed, then error information will be available in the FileOpenError and FileOpenErrorMsg properties. Returns true for success, false for failure.
public string ReadBinaryToEncoded(string filePath, string encoding);
Reads the entire contents of a binary file and returns it as an encoded string (using an encoding such as Base64, Hex, etc.) The encoding may be one of the following strings: base64, hex, qp, or url. Returns a null on failure
public byte[] ReadEntireFile(string filePath);
Reads the entire contents of a binary file and returns the data. Returns an empty byte array on failure
public string ReadEntireTextFile(string filePath, string charset);
Reads the entire contents of a text file, interprets the bytes according to the character encoding specified by charset, and returns the text file as a string. Returns a null on failure
public bool ReassembleFile(string partsDirPath, string partPrefix, string partExtension, string reassembledFilename);
Reassembles a file previously split by the SplitFile method. (C#) Example: Reassemble a Previously Split File
public bool SaveLastError(string filename);
Saves the last error information to an XML formatted file.
public bool SetCurrentDir(string dirPath);
Sets the current working directory for the calling process to dirPath.
public bool SplitFile(string fileToSplit, string partPrefix, string partExtension, int partSize, string outputDirPath);
Splits a file into chunks. Please refer to the example below: (C#) Example: Split File into Chunks
public bool TreeDelete(string path);
Deletes an entire directory tree (all files and sub-directories). Returns true for success, false for failure.
public bool WriteEntireFile(string filePath, byte[] fileData);
Opens/creates filePath, writes fileData, and closes the file. Returns true for success, false for failure.
public bool WriteEntireTextFile(string filePath, string textData, string charset, bool includedPreamble);
Opens filePath, writes textData using the character encoding specified by charset, and closes the file. If includedPreamble is true and the charset is Unicode or utf-8, then the BOM is included at the beginning of the file. Returns true for success, false for failure.
|