Path.TryJoin 方法

定义

尝试将各个路径组件连接到预分配的字符范围,并返回一个值指示操作是否成功。Attempts to concatenate individual path components to a preallocated character span, and returns a value that indicates whether the operation succeeded.

重载

TryJoin(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, Span<Char>, Int32)

尝试将两个路径组件连接到一个预分配的字符范围,并返回一个值,该值指示操作是否成功。Attempts to concatenate two path components to a single preallocated character span, and returns a value that indicates whether the operation succeeded.

TryJoin(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Span<Char>, Int32)

尝试将两个路径组件连接到一个预分配的字符范围,并返回一个值,该值指示操作是否成功。Attempts to concatenate two path components to a single preallocated character span, and returns a value that indicates whether the operation succeeded.

注解

目标字符跨度必须足够大才能容纳连接的路径。The destination character span must be large enough to hold the concatenated path. 然后,可以通过调用方法来检索串联路径 Span<T>.Slice ,如下面的示例所示。You can then retrieve the concatenated path by calling the Span<T>.Slice method, as the following example illustrates.

using System;
using System.IO;

class Program
{
    static void Main()
    {
        int nChars = 0;
        var buffer = new Span<Char>(new String(' ', 100).ToCharArray());
        var flag = Path.TryJoin("C:/".AsSpan(), "Users/user1".AsSpan(), buffer, out nChars);
        if (flag) 
            Console.WriteLine($"Wrote {nChars} characters: '{buffer.Slice(0, nChars).ToString()}'");
        else
            Console.WriteLine("Concatenation operation failed.");
    }
}

TryJoin(ReadOnlySpan<Char>, ReadOnlySpan<Char>, ReadOnlySpan<Char>, Span<Char>, Int32)

尝试将两个路径组件连接到一个预分配的字符范围,并返回一个值,该值指示操作是否成功。Attempts to concatenate two path components to a single preallocated character span, and returns a value that indicates whether the operation succeeded.

public:
 static bool TryJoin(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, ReadOnlySpan<char> path3, Span<char> destination, [Runtime::InteropServices::Out] int % charsWritten);
public static bool TryJoin (ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, ReadOnlySpan<char> path3, Span<char> destination, out int charsWritten);
static member TryJoin : ReadOnlySpan<char> * ReadOnlySpan<char> * ReadOnlySpan<char> * Span<char> * int -> bool
Public Shared Function TryJoin (path1 As ReadOnlySpan(Of Char), path2 As ReadOnlySpan(Of Char), path3 As ReadOnlySpan(Of Char), destination As Span(Of Char), ByRef charsWritten As Integer) As Boolean

参数

path1
ReadOnlySpan<Char>

包含要加入的第一个路径的字符范围。A character span that contains the first path to join.

path2
ReadOnlySpan<Char>

包含要加入的第二个路径的字符范围。A character span that contains the second path to join.

path3
ReadOnlySpan<Char>

包含要加入的第三个路径的字符范围。A character span that contains the third path to join.

destination
Span<Char>

用于保存连接的路径的字符范围。A character span to hold the concatenated path.

charsWritten
Int32

方法返回时,为一个指示写入 destination 的字符数的值。When the method returns, a value that indicates the number of characters written to the destination.

返回

Boolean

如果此串联运算成功,则为 true;否则为 falsetrue if the concatenation operation is successful; otherwise, false.

注解

destination 必须足够大才能容纳连接的路径。destination must be large enough to hold the concatenated path. 然后,可以通过调用方法来检索串联路径 Span<T>.Slice ,如示例所示。You can then retrieve the concatenated path by calling the Span<T>.Slice method, as the example illustrates.

另请参阅

适用于

TryJoin(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Span<Char>, Int32)

尝试将两个路径组件连接到一个预分配的字符范围,并返回一个值,该值指示操作是否成功。Attempts to concatenate two path components to a single preallocated character span, and returns a value that indicates whether the operation succeeded.

public:
 static bool TryJoin(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, Span<char> destination, [Runtime::InteropServices::Out] int % charsWritten);
public static bool TryJoin (ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, Span<char> destination, out int charsWritten);
static member TryJoin : ReadOnlySpan<char> * ReadOnlySpan<char> * Span<char> * int -> bool
Public Shared Function TryJoin (path1 As ReadOnlySpan(Of Char), path2 As ReadOnlySpan(Of Char), destination As Span(Of Char), ByRef charsWritten As Integer) As Boolean

参数

path1
ReadOnlySpan<Char>

包含要加入的第一个路径的字符范围。A character span that contains the first path to join.

path2
ReadOnlySpan<Char>

包含要加入的第二个路径的字符范围。A character span that contains the second path to join.

destination
Span<Char>

用于保存连接的路径的字符范围。A character span to hold the concatenated path.

charsWritten
Int32

方法返回时,为一个指示写入 destination 的字符数的值。When the method returns, a value that indicates the number of characters written to the destination.

返回

Boolean

如果此串联运算成功,则为 true;否则为 falsetrue if the concatenation operation is successful; otherwise, false.

注解

destination 必须足够大才能容纳连接的路径。destination must be large enough to hold the concatenated path. 然后,可以通过调用方法来检索串联路径 Span<T>.Slice ,如示例所示。You can then retrieve the concatenated path by calling the Span<T>.Slice method, as the example illustrates.

另请参阅

适用于