SoFunction
Updated on 2025-03-01

Detailed explanation of C# function case

Use of () functions in C#

In C# (string path, string searchPattern, SearchOption searchOption)

Get all files in the path directory

Note: The red font part is optional parameters

parameter

path

Relative or absolute path to the directory to search for. This string is case-insensitive.

searchPattern

The search string to match the file name in path. This parameter can contain a combination of valid text paths and wildcards (* and ?) (see "Notes"), but regular expressions are not supported.

SearchPattern can be a combination of text and wildcard characters, but does not support regular expressions. The following wildcard specifier is allowed to be used in searchPattern.

Wildcard specifier match
* (Asterisk) Zero or more characters at that position.
?(question mark) Zero or one character at that position.

For details, please refer to:/zh-cn/library/ms143316(v=vs.110).aspxAfter testing, it was found that "*.mat" can search for files in formats such as "", "box.mat1", etc., but the file cannot be searched for "" searchOption

Used to specify whether the search operation should contain all subdirectories or only one of the enumeration values ​​of the current directory.

The code is as follows:

using System;
using ;
namespace 
{
 [ComVisible (true)]
 [Serializable]
 public enum SearchOption
 {
  TopDirectoryOnly,
  AllDirectories
 }
}

Default option, including only the current directory

Contains all subdirectories

Return value

Type: []

Specifies an array of the full name (including paths) of the files in the directory that match the specified search pattern and options; if no files are found, an empty array.

1. Use relative paths

string path = "Assets/model";

string[] files = (path) ;

The current path can be viewed through ().

2. Use absolute paths

string path = "D:/UnityDemo/Assets/model"

string[] files = (path)

This is the end of this article about the detailed explanation of C# () function cases. For more related C# () function content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!