开发者

C#'s equivalent to Python's os.path.splitext()?

With Python, it has os.path.splitext() to replace the extension name to something as follows.

import os.path
names = os.path.splitext('hello.exe')
prin开发者_如何学Pythont names[0] + ".coverage"

And I have hello.coverage.

Does C# has equivalent functions as python's os.path?

ANSWERED

using System;
using System.IO;

namespace Hello 
{
    class Code 
    {
        static void Main(string[] args)
        {
            string path = "hello.exe";
            string newPath = Path.ChangeExtension(path, ".coverage");

            Console.WriteLine("{0} - {1}", path, newPath);
        }
    }
}


System.IO.Path.ChangeExtension does what you want:

string path = "hello.exe";
string newPath = System.IO.Path.ChangeExtension(path, ".coverage");

EDIT: Your using statement should be using System.IO, and then you can use it as you have it or like this:

Path.ChangeExtension(path, newExtension);

Also, Console.WriteLine can't be used as you have it. You could do this though:

Console.WriteLine("{0} - {1}", path, newPath);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜