Removes extension from path. - CSharp System.IO

CSharp examples for System.IO:File Path

Description

Removes extension from path.

Demo Code

// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

public class Main{
        /// <summary>
        /// Removes extension from path.
        /// </summary>
        /// <remarks>
        /// Returns "foo" for path "foo.".
        /// Returns "foo.." for path "foo...".
        /// </remarks>
        private static string RemoveExtension(string path)
        {//w  w w . j a  va  2 s  .  c  o  m
            if (path == null)
            {
                return null;
            }

            int index = IndexOfExtension(path);
            if (index >= 0)
            {
                return path.Substring(0, index);
            }

            // trim last ".", if present
            if (path.Length > 0 && path[path.Length - 1] == '.')
            {
                return path.Substring(0, path.Length - 1);
            }

            return path;
        }
}

Related Tutorials