Path.ChangeExtension(String, String) メソッド
定義
パス文字列の拡張子を変更します。Changes the extension of a path string.
public:
static System::String ^ ChangeExtension(System::String ^ path, System::String ^ extension);
public static string ChangeExtension (string path, string extension);
static member ChangeExtension : string * string -> string
Public Shared Function ChangeExtension (path As String, extension As String) As String
パラメーター
- path
- String
変更するパス情報。The path information to modify. パスに、GetInvalidPathChars() で定義された文字を含めることはできません。The path cannot contain any of the characters defined in GetInvalidPathChars().
- extension
- String
新しい拡張子 (先行ピリオド付き、またはなし)。The new extension (with or without a leading period). null
を指定して、path
から既存の拡張子を削除します。Specify null
to remove an existing extension from path
.
戻り値
変更されたパス情報。The modified path information.
Windows ベースのデスクトップ プラットフォームでは、path
が null
または空の文字列 ("") の場合、パス情報は変更されずに返されます。On Windows-based desktop platforms, if path
is null
or an empty string (""), the path information is returned unmodified. extension
が null
の場合は、返される文字列に、削除した拡張子が付いた指定したパスが含まれます。If extension
is null
, the returned string contains the specified path with its extension removed. path
に拡張子がなく、extension
が null
でない場合は、返されるパス文字列に path
の末尾に追加される extension
が含まれます。If path
has no extension, and extension
is not null
, the returned path string contains extension
appended to the end of path
.
例外
path
が、GetInvalidPathChars() で定義されている無効な文字を 1 つ以上含んでいます。path
contains one or more of the invalid characters defined in GetInvalidPathChars().
例
次の例は、メソッドの使用方法を示して ChangeExtension
います。The following example demonstrates a use of the ChangeExtension
method.
#using <system.dll>
using namespace System;
using namespace System::IO;
void ChangeExtension()
{
String^ goodFileName = "C:\\mydir\\myfile.com.extension";
String^ badFileName = "C:\\mydir\\";
String^ result;
result = Path::ChangeExtension( goodFileName, ".old" );
Console::WriteLine( "ChangeExtension({0}, '.old') returns '{1}'", goodFileName, result );
result = Path::ChangeExtension( goodFileName, "" );
Console::WriteLine( "ChangeExtension({0}, '') returns '{1}'", goodFileName, result );
result = Path::ChangeExtension( badFileName, ".old" );
Console::WriteLine( "ChangeExtension({0}, '.old') returns '{1}'", badFileName, result );
// This code produces output similar to the following:
//
// ChangeExtension(C:\mydir\myfile.com.extension, '.old') returns 'C:\mydir\myfile.com.old'
// ChangeExtension(C:\mydir\myfile.com.extension, '') returns 'C:\mydir\myfile.com.'
// ChangeExtension(C:\mydir\, '.old') returns 'C:\mydir\.old'
using System;
using System.IO;
public class PathSnippets
{
public void ChangeExtension()
{
string goodFileName = @"C:\mydir\myfile.com.extension";
string badFileName = @"C:\mydir\";
string result;
result = Path.ChangeExtension(goodFileName, ".old");
Console.WriteLine("ChangeExtension({0}, '.old') returns '{1}'",
goodFileName, result);
result = Path.ChangeExtension(goodFileName, "");
Console.WriteLine("ChangeExtension({0}, '') returns '{1}'",
goodFileName, result);
result = Path.ChangeExtension(badFileName, ".old");
Console.WriteLine("ChangeExtension({0}, '.old') returns '{1}'",
badFileName, result);
// This code produces output similar to the following:
//
// ChangeExtension(C:\mydir\myfile.com.extension, '.old') returns 'C:\mydir\myfile.com.old'
// ChangeExtension(C:\mydir\myfile.com.extension, '') returns 'C:\mydir\myfile.com.'
// ChangeExtension(C:\mydir\, '.old') returns 'C:\mydir\.old'
Imports System.IO
Public Class PathSnippets
Public Sub ChangeExtension()
Dim goodFileName As String = "C:\mydir\myfile.com.extension"
Dim badFileName As String = "C:\mydir\"
Dim result As String
result = Path.ChangeExtension(goodFileName, ".old")
Console.WriteLine("ChangeExtension({0}, '.old') returns '{1}'", goodFileName, result)
result = Path.ChangeExtension(goodFileName, "")
Console.WriteLine("ChangeExtension({0}, '') returns '{1}'", goodFileName, result)
result = Path.ChangeExtension(badFileName, ".old")
Console.WriteLine("ChangeExtension({0}, '.old') returns '{1}'", badFileName, result)
' This code produces output similar to the following:
'
' ChangeExtension(C:\mydir\myfile.com.extension, '.old') returns 'C:\mydir\myfile.com.old'
' ChangeExtension(C:\mydir\myfile.com.extension, '') returns 'C:\mydir\myfile.com.'
' ChangeExtension(C:\mydir\, '.old') returns 'C:\mydir\.old'
注釈
との path
どちら extension
にもピリオド (.) が含まれていない場合は、 ChangeExtension
期間が追加されます。If neither path
nor extension
contains a period (.), ChangeExtension
adds the period.
パラメーターには、 extension
複数のピリオドと有効なパス文字を含めることができ、任意の長さを指定できます。The extension
parameter can contain multiple periods and any valid path characters, and can be any length. がの場合 extension
null
、返される文字列には、 path
最後の期間に続くの内容と、その後のすべての文字が含まれます。If extension
is null
, the returned string contains the contents of path
with the last period and all characters following it removed.
extension
が空の文字列の場合、返されるパス文字列には、 path
最後の期間が削除された後の文字を含むの内容が含まれます。If extension
is an empty string, the returned path string contains the contents of path
with any characters following the last period removed.
に path
拡張子がなく extension
、がでない場合 null
、返される文字列にはが path
続き extension
ます。If path
does not have an extension and extension
is not null
, the returned string contains path
followed by extension
.
がではなく、先頭のピリオドが含まれていない場合は、 extension
null
期間が追加されます。If extension
is not null
and does not contain a leading period, the period is added.
に path
複数のピリオドで区切られた複数の拡張機能が含まれている場合、返される文字列には、 path
最後のピリオドとそれに続くすべての文字がで置き換えられたの内容が含まれ extension
ます。If path
contains a multiple extension separated by multiple periods, the returned string contains the contents of path
with the last period and all characters following it replaced by extension
. たとえば、 path
が "\Dir1\examples\pathtests.csx.txt" で、 extension
が "cs" の場合、変更されたパスは "\Dir1\examples\pathtests.csx.cs" になります。For example, if path
is "\Dir1\examples\pathtests.csx.txt" and extension
is "cs", the modified path is "\Dir1\examples\pathtests.csx.cs".
返された結果がすべてのシナリオで有効であることを確認することはできません。It is not possible to verify that the returned results are valid in all scenarios. たとえば、 path
が空の場合、 extension
が追加されます。For example, if path
is empty, extension
is appended.
共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。For a list of common I/O tasks, see Common I/O Tasks.