How to get the filename according to my requirement C# DirctoryInfo.GetFileSystemInfos(); [duplicate]

StackOverflow https://stackoverflow.com/questions/16979522

  •  31-05-2022
  •  | 
  •  

質問

. net winform

Now I get all the fileName under the specified directory. the result is "0001_00001523_028155.sql"

but my goal is only to get "00001523"

how can I do that?

private void loadscriptfolder()
    {
        string folderName = this.textBoxScriptLocation.Text.Trim();
        DirectoryInfo dir = new DirectoryInfo(folderName);
        if (dir.Exists)
        {
            FileSystemInfo[] fs = dir.GetFileSystemInfos();
            foreach (FileSystemInfo fs2 in fs)
            {
                FileInfo file = fs2 as FileInfo;
                if (file != null)
                {
                    listBoxResult.Items.Add(file);
                }                    
            }                
        }
    }

Thank you everyone!!!

役に立ちましたか?

解決

If you just really need to get that part of the file name, then you can just do a string manipulation on its file name.

                FileInfo file = fs2 as FileInfo;
                if (file != null)
                {
                    listBoxResult.Items.Add(file.Name.Split('_')[1]);
                }

他のヒント

Try

    string str = @"0001_00001523_028155.sql";
    var result = str.Split('_')[1];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top