str_mouth = DateTime.Now.ToString("yyyy-MM-dd"); //获取当前日期
fullPath = System.AppDomain.CurrentDomain.BaseDirectory + @"Pyxis" + str_mouth + "-" + 0 + ".csv";//System.AppDomain.CurrentDomain.BaseDirectory:获取程序的基目录
FileInfo fi = new FileInfo(fullPath);//创建名为fi文件
int i = 0; //文件后缀编号
if (!fi.Directory.Exists)
fi.Directory.Create();//文件不存在创建一个文件
while (File.Exists(fullPath))//确定指定的文件是否存在
{
fullPath = AppDomain.CurrentDomain.BaseDirectory + @"Pyxis" + str_mouth + "-" + i.ToString() + ".csv"; //文件名
i++;
}
Directory获取文件父目录的实例
.Exists获取文件是否存在的值,如果该文件存在,则为 true;如果文件不存在或文件即是目录,则为 false。
参考:https://docs.microsoft.com/zh-cn/dotnet/api/system.io.fileinfo?view=netframework-4.7.2