Пример #1
0
bool File::Delete(const String &path)
{
	ManagedObject<FileStream> file = File::Open(path, 
												FileMode::OpenExist, 
												FileAccess::NT_DELETE);
    return file.IsNull() ? false : file->Delete();
}
Пример #2
0
FileStream *File::Create(const String &path,
						 FileMode mode /* = FileMode::CreateOnly */, 
						 FileAccess access /* = FileAccess::ReadWrite */, 
						 FileAttributes attr /* = FileAttributes::Normal */, 
						 FileShare share /* = FileShare::None */, 
						 FileOptions options /* = FileOptions::None */)
{
	ManagedObject<FileStream> file = new FileStream;
	if (file.IsNull()) {
		Exception::SetLastException(STATUS_NO_MEMORY, false);
		return nullptr;
	} //if

	  // converts to full nt path
	TCHAR fullpath[RLIB_MAX_PATH];
	StringCopyToW(Path::ToNtPath(path), fullpath);

	UNICODE_STRING us_path;
	RtlInitUnicodeString(&us_path, fullpath);

	// others parameters
	OBJECT_ATTRIBUTES obj_attr;
	InitializeObjectAttributes(&obj_attr, &us_path, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE,
							   NULL, NULL);

	IO_STATUS_BLOCK io_stat;
	NTSTATUS status = NtCreateFile(&file->m_native_handle,
								   static_cast<ACCESS_MASK>(access), 
								   &obj_attr, 
								   &io_stat, 
								   NULL,
								   static_cast<ULONG>(attr), 
								   static_cast<ULONG>(share),
								   static_cast<ULONG>(mode != FileMode::AppendAlways ? mode : FileMode::OpenOrCreate),
								   static_cast<ULONG>(options), 
								   NULL,
								   NULL);
	if (status == STATUS_SUCCESS) {
		if (mode == FileMode::AppendAlways) {
			file->Position = file->Length;
		} //if
		return file.SuppressFinalize();
	} //if

	file->m_native_handle = INVALID_HANDLE_VALUE;
	Exception::SetLastException(status);

    return nullptr;
}