Exemplo n.º 1
0
        private static string GetFilePathOrNameDetailByOption(string filePathOrName, FilePathComposition returnOpt)  //1:fileTitle;2:postfix;3:suffix;
        {
            int lastIndexOfDot = filePathOrName.LastIndexOf(".");

            if (lastIndexOfDot == -1)
            {
                throw new ArgumentException("Filename must contains separator  '.'");
            }
            string fileTitle = filePathOrName.Substring(0, lastIndexOfDot);
            int    lastIndexOfDoubleSlash = fileTitle.LastIndexOf("\\");

            if (lastIndexOfDoubleSlash != -1)
            {
                fileTitle = fileTitle.Substring(lastIndexOfDoubleSlash + 1, fileTitle.Length - lastIndexOfDoubleSlash - 1);
            }
            string suffix  = filePathOrName.Substring(lastIndexOfDot + 1, filePathOrName.Length - lastIndexOfDot - 1);
            string postfix = "." + suffix;

            if (returnOpt == FilePathComposition.FileTitle)
            {
                return(fileTitle);
            }
            else if (returnOpt == FilePathComposition.Postfix)
            {
                return(postfix);
            }
            else if (returnOpt == FilePathComposition.Suffix)
            {
                return(suffix);
            }
            else
            {
                return(string.Empty);
            }
        }
Exemplo n.º 2
0
        public static string GetFileDetailByOption(string path, FilePathComposition returnOpt)  //1:dir;2:fileName;
        {
            int lastIndex = path.LastIndexOf("\\");
            var dir       = path.Substring(0, lastIndex);
            var fileName  = path.Substring(lastIndex + 1, path.Length - lastIndex - 1);

            if (returnOpt == FilePathComposition.Directory)
            {
                return(dir);                                           //dir
            }
            else if (returnOpt == FilePathComposition.FileName)
            {
                return(fileName);                                                //file name
            }
            else if (returnOpt == FilePathComposition.FileTitle)
            {
                if (fileName.Contains('.'))
                {
                    return(GetFilePathOrNameDetailByOption(path, FilePathComposition.FileTitle));
                }
                else
                {
                    return(fileName);
                }
            }
            else if (returnOpt == FilePathComposition.Postfix)
            {
                if (fileName.Contains('.'))
                {
                    return(GetFilePathOrNameDetailByOption(path, FilePathComposition.Postfix));
                }
                else
                {
                    return(string.Empty);
                }
            }
            else if (returnOpt == FilePathComposition.Suffix)
            {
                ;
            }
            {
                if (fileName.Contains('.'))
                {
                    return(GetFilePathOrNameDetailByOption(path, FilePathComposition.Suffix));
                }
                else
                {
                    return(string.Empty);
                }
            }
            return(string.Empty);
        }