[C#]不依赖第三方库实现同时可以选择文件或者文件夹到texbox

 OpenFileDialog ofd = new OpenFileDialog();
        ofd.CheckFileExists = false;
        string defaultFilename = "Select this folder";
        ofd.FileName = defaultFilename;

        if (ofd.ShowDialog().Value)
        {
            // Check if the user picked a file or a directory, for example:
            if (!ofd.FileName.Contains(defaultFilename))
            {
                // File code
            }
            else // You should probably turn this into an else if instead
            {
                // Directory code
            }
            // Alternatively, but still as unsafe
            if (File.Exists(ofd.FileName))
            {
                // File code
            }
            else
            {
                // Directory code
            }
        }

你可能感兴趣的:(C#,c#,开发语言)