0

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();
4

3 回答 3

1

我仍然无法理解,您从头到尾都在尝试完成什么,但是..如果您有一组完整路径并且您只需要从这些路径中获取文件名,您可以执行以下操作:

实际上files可能包含随机的、绝对不同的路径,但根据我从问题中得到的信息,它是:

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

然后,您可以使用Path.GetFileName 方法通过简单的Enumerable.Select LINQ 语句从这些路径中仅检索文件名:

var fileNamesOnly = files.Select(f => Path.GetFileName(f));
于 2012-12-24T06:13:05.717 回答
1

我不完全确定你到底需要什么。对于你的句子:

字符串是 currentDir 我需要从 images[] 中的每个元素中修剪

您可以使用 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();

或使用string.TrimStart

string[] newImages = images.Select(r => r.TrimStart(currDir.ToCharArray())).ToArray();
于 2012-12-24T04:47:26.450 回答
1

对不起,但我不清楚......如果你只想要整个路径中的文件名,那么你可以简单地使用 Split ,用特殊字符分割整个路径并使用最后一个数组元素。

一旦您获得“图像”数组中的所有路径,您可以尝试下面的代码。

例如:-

    for(i=0;i<images.length;i++)
    {
     string [] cuttofilename=images[i].split('\');
     string filename=cuttofilename[cuttofilename.lentgh-1];
    }
于 2012-12-24T06:01:20.547 回答