file模块

getAttributes函数

uint getAttributes(R)(R name)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);

uint getAttributes(R)(auto ref R name)
if (isConvertibleToString!R);

返回给定文件的属性。请注意,在Windows和POSIX系统上,文件属性是完全不同的。在Windows上,它们是由GetFileAttributes返回的值,而在POSIX系统上,它们是通过调用stat函数获得的stat结构的st_mode值。

在POSIX系统上,如果给定的文件是符号链接,则属性是指向符号链接的文件的属性。

参数:

  • name:要获取属性的文件。

返回:

  • 文件属性的uint值。

抛出:

  • 在出现错误时抛出FileException

示例:

获取文件的属性:

import std.exception : assertThrown;

auto f = deleteme ~ "file";
scope(exit) f.remove;

assert(!f.exists);
assertThrown!FileException(f.getAttributes);

f.write(".");
auto attributes = f.getAttributes;
assert(!attributes.attrIsDir);
assert(attributes.attrIsFile);

获取目录的属性:

import std.exception : assertThrown;

auto dir = deleteme ~ "dir";
scope(exit) dir.rmdir;

assert(!dir.exists);
assertThrown!FileException(dir.getAttributes);

dir.mkdir;
auto attributes = dir.getAttributes;
assert(attributes.attrIsDir);
assert(!attributes.attrIsFile);

getLinkAttributes函数

uint getLinkAttributes(R)(R name)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);

uint getLinkAttributes(R)(auto ref R name)
if (isConvertibleToString!R);

如果给定的文件是符号链接,则返回符号链接本身的属性,而不是它指向的文件的属性。如果给定的文件不是符号链接,则此函数返回与getAttributes相同的结果。

在Windows上,getLinkAttributesgetAttributes相同。它存在于Windows上,因此在处理符号链接时无需特别处理Windows的代码。

参数:

  • name:要获取符号链接属性的文件。

返回:

  • 属性的uint值。

抛出:

  • 在出现错误时抛出FileException

示例:

获取符号链接的属性:

import std.exception : assertThrown;

auto source = deleteme ~ "source";
auto target = deleteme ~ "target";

assert(!source.exists);
assertThrown!FileException(source.getLinkAttributes);

version (Posix)
{
    scope(exit) source.remove, target.remove;

    target.write("target");
    target.symlink(source);
    writeln(source.readText); // "target"
    assert(source.isSymlink);
    assert(source.getLinkAttributes.attrIsSymlink);
}

如果文件不是符号链接,则getLinkAttributes的行为与getAttributes相同:

import std.exception : assertThrown;

auto f = deleteme ~ "file";
scope(exit) f.remove;

assert(!f.exists);
assertThrown!FileException(f.getLinkAttributes);

f.write(".");
auto attributes = f.getLinkAttributes;
assert(!attributes.attrIsDir);
assert(attributes.attrIsFile);

setAttributes函数

void setAttributes(R)(R name, uint attributes)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);

void setAttributes(R)(auto ref R name, uint attributes)
if (isConvertibleToString!R);

设置给定文件的属性。例如,Unix的chmod +x name的程序等效于name.setAttributes(name.getAttributes | octal!700)

参数:

  • name:文件名。
  • attributes:要设置文件的属性。

抛出:

  • 如果给定的文件不存在,则抛出FileException

示例:

设置文件的属性:

import std.exception : assertThrown;
import std.conv : octal;

auto f = deleteme ~ "file";
version (Posix)
{
    scope(exit) f.remove;

    assert(!f.exists);
    assertThrown!FileException(f.setAttributes(octal!777));

    f.write(".");
    auto attributes = f.getAttributes;
    assert(!attributes.attrIsDir);
    assert(attributes.attrIsFile);

    f.setAttributes(octal!777);
    attributes = f.getAttributes;

    writeln((attributes & 1023)); // octal!777
}

设置目录的属性:

import std.exception : assertThrown;
import std.conv : octal;

auto dir = deleteme ~ "dir";
version (Posix)
{
    scope(exit) dir.rmdir;

    assert(!dir.exists);
    assertThrown!FileException(dir.setAttributes(octal!777));

    dir.mkdir;
    auto attributes = dir.getAttributes;
    assert(attributes.attrIsDir);
    assert(!attributes.attrIsFile);

    dir.setAttributes(octal!777);
    attributes = dir.getAttributes;

    writeln((attributes & 1023)); // octal!777
}
``
关于dlang语言的file模块以下内容请用好看的markdown翻译成中文内容如下:uint getAttributesRR nameif isSomeFiniteCharInputRange!R && !isConvertibleToString!R;uint getAttributesRauto ref R nameif isConvertibleToString!R;Returns the

原文地址: http://www.cveoy.top/t/topic/gSuN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录