using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AssistClient.Utils
{
internal class FileUtil
{
///
/// 另存为
///
/// 默认文件名
/// 如 图像文件|*.jpg|图像文件|*.png|所有文件|*.*
///
public static string saveAsFile(string defaultFileName, string filter= "所有文件|*.*")
{
SaveFileDialog s = new SaveFileDialog();
s.Title = "保存文件";
s.Filter = filter; //"图像文件|*.jpg|图像文件|*.png|所有文件|*.*";
//s.DefaultExt = "图像文件|*.jpg"; //默认扩展名
//s.InitialDirectory = @"C:\Users\Administrator\Desktop"; //保存的初始目录
s.FileName = defaultFileName;//默认文件名
if (s.ShowDialog() == DialogResult.OK)
return s.FileName;
return "";
}
///
/// 选择文件
///
/// 如 图像文件|*.jpg|图像文件|*.png|所有文件|*.*
///
public static string openFile(string filter = "所有文件|*.*", string defaultPath = "")
{
OpenFileDialog fie = new OpenFileDialog();
fie.Title = "选择文件";
//fie.InitialDirectory = (defaultPath != "" ? defaultPath : Path.GetFullPath("."));
fie.Filter = filter; //设置文件类型
if (fie.ShowDialog() == DialogResult.OK)
return fie.FileName;
return "";
}
///
/// 选择目录
///
///
public static string selectFolder(string defaultPath="")
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.SelectedPath = (defaultPath != "" ? defaultPath : Path.GetFullPath("."));
if (dlg.ShowDialog() == DialogResult.OK)
return dlg.SelectedPath;
return "";
}
///
/// 写INI文件
///
/// Section
/// Key
/// value
public static void WriteIniValue(string filePath, string Section, string Key, string value)
{
WINAPI.WritePrivateProfileString(Section, Key, value, filePath);
}
///
/// 读取INI文件指定部分
///
/// Section
/// Key
/// String
public static string ReadIniValue(string filePath, string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = WINAPI.GetPrivateProfileString(Section, Key, "", temp, 255, filePath);
return temp.ToString().Trim();
}
}
}