C# 基础

using System.Windows.Forms;

...

/// <summary>  
/// 选择保存文件的名称以及路径  取消返回 空"";  
/// </summary>  
/// <param name="fileName"></param>  
/// <param name="filter"></param>  
/// <param name="title"></param>  
/// <returns></returns>  
public static string GetSaveFilePath(string fileName = null, string filter = null, string title = null)
{
    string path = "";
    SaveFileDialog savedialog = new SaveFileDialog();
    savedialog.AddExtension = true;
    if (string.IsNullOrEmpty(fileName)) savedialog.FileName = DateTime.Now.ToString("yyyyMMdd"); else savedialog.FileName = fileName;
    if (string.IsNullOrEmpty(filter)) savedialog.Filter = "All File|*.*"; else savedialog.Filter = filter;
    if (string.IsNullOrEmpty(title)) savedialog.Title = "保存为"; else savedialog.Title = title;

    if (savedialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
    	path = savedialog.FileName;
    }
    return path;
}

/// <summary>
/// 选择一个文件
/// </summary>
/// <param name="filter">文件类型筛选器</param>
/// <returns>选择文件完整路径,null没有选择</returns>
public static string SelectFiles(string filter)
{
    string[] paths = null;
    string[] names = null;
    // 弹出文件选择框
    if (SelectFiles(filter,false,out paths,out names))
    {
    	return paths[0];
    }
    else
    {
    	return null;
    }
}

/// <summary>
/// 选择多个文件
/// </summary>
/// <param name="filter">文件类型筛选器</param>
/// <param name="multiselect">是否可以选择多个文件</param>
/// <param name="fullpaths">文件完整路径数组</param>
/// <param name="names">文件名数组</param>
/// <returns>是否选择成功</returns>
public static bool SelectFiles(string filter,bool multiselect,out string[] fullpaths,out string[] names)
{
    // 初始化
    bool result = false;
    fullpaths = null;
    names = null;

    OpenFileDialog selectdialog = new OpenFileDialog();
    selectdialog.Multiselect = multiselect;
    if (string.IsNullOrEmpty(filter)) selectdialog.Filter = "All File|*.*"; else selectdialog.Filter = filter;
    // 弹出文件选择框
    result = selectdialog.ShowDialog()== System.Windows.Forms.DialogResult.OK)
    if(result)
    {
    	fullpaths = selectdialog.FileNames;
    	names = selectdialog.SafeFileNames;
    }
    return result;
}
原文地址:https://www.cnblogs.com/MichaelLoveSna/p/14475241.html