简体   繁体   中英

string manipulation using Directory.GetFiles()

i am assigning images[] with an array that holds images file names with full path of given Directory.

string[] images = DirLoad.FileNamesArray(
                                          IO.Loaders.PathType.full, 
                                          IO.Loaders.FileExtension.jpg
                                        );

...now, that images[] stores all the file names i need, as I had to use the full path to get it done,

using Directory.GetFiles()

Next action requires it as a local file name .

(each is then passed as string type parameter to another method)

so my question is :

How can i omit first part - HttpRuntime.AppDomainAppPath ...if it's same in every element of array ?

this is usage example, the string is currentDir i need to trim from each element in images[]

    public class IO
    {
        public class Loaders
        {
            readonly string currentDir = HttpRuntime.AppDomainAppPath;

            public string selecedDirName { get; set; }
            /// <summary>
            /// assign The Loaders.selectedDir First before calling
            /// </summary>
            /// <param name="foldertoLoad"></param>
            /// <returns></returns>
            public enum PathType
            {
                full, local
            }
            public enum FileExtension
            {
                jpg,png,txt,xml,htm,js,aspx,css
            }
            public string[] FileNamesArray(PathType SelectedPathMode, FileExtension selectedfileType)
            {
                string thisFolder = "";
                string thatFileType= string.Format("*.{0}",selectedfileType.ToString());
                switch (SelectedPathMode)
                {
                    case PathType.full:
                        thisFolder = Path.Combine(currentDir, selecedDirName);
                        break;
                    case PathType.local:
                        thisFolder = selecedDirName;
                        break;
                    default:
                        break;
                }

                string[] foundArr = Directory.GetFiles(thisFolder, thatFileType);
                return foundArr;
            }
        }

    }

Update , this is what i've tried

string fileName;
string[] images = DirLoad.FilesArray(IO.Loaders.PathType.full, IO.Loaders.FileExtention.jpg);
        foreach (var currImage in images)
        {
              int startingAt = DirLoad.currentDir.Length ;
              int finalPoint = currImage.Length - startingAt;
              fileName = new String(currImage.ToCharArray(startingAt, finalPoint));
              baseStyle.Add(string.Format("{0}url({1}) {2}", BackGroundCssProp, fileName, imageProps));
        }

        return baseStyle.ToArray();

I am not entirely sure what you exactly need. For your sentence:

the string is currentDir i need to trim from each element in images[]

You can try the following using LINQ:

string currDir = "SomeString";
string[] images = new string[] { "SomeStringabc1.jpg", "SomeStringabc2.jpg", "SomeStringabc3.jpg", "abc.jpg" };
string[] newImages = images.Select(r => r.StartsWith(currDir) 
                                                ? r.Replace(currDir, "") : r)
                            .ToArray();

Or using string.TrimStart

string[] newImages = images.Select(r => r.TrimStart(currDir.ToCharArray())).ToArray();

sorry but it is not clear to me... if you want only the filename from whole path then you can simply use Split for it, split the whole path with special character and use last array element.

once you will get all the path in your "images" array you can try below code.

for example:-

    for(i=0;i<images.length;i++)
    {
     string [] cuttofilename=images[i].split('\');
     string filename=cuttofilename[cuttofilename.lentgh-1];
    }

Still I fail to understand, what you're trying to accomplish from the beginning to the end, but..If you are having an array of full paths and you need to get only filenames from these paths, you can do the following:

Actually files may contain random, absolutely different paths, but according to what I have caught from the question, et it be:

var files = Directory.GetFiles(@"path"); 

Then you may use Path.GetFileName Method to retrieve only filename from these paths, through a simple Enumerable.Select LINQ-statement:

var fileNamesOnly = files.Select(f => Path.GetFileName(f));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM