我正在使用 C# 创建一个临时文件。
public partial class frmResults : Form
{
public static string caseFile = "";
private void frmResults_Load(object sender, EventArgs e)
{
caseFile = CreateTempFiles();
FileInfo file = new FileInfo(caseFile + ".rpt");
if (file.Exists)
{
try
{
if (fil.Length < 64000000)
{
richTabular.LoadFile(caseFile + ".rpt", RichTextBoxStreamType.PlainText);
}
}
catch (IOException io)
{
MessageBox.Show(io.GetType().Name);
}
catch (Exception ex)
{
throw ex;
}
}
public static string CreateTempFiles()
{
string sPath;
caseFile = Path.GetTempFileName();
sPath = Path.GetTempPath();
string workDir = sPath + "\\work\\";
// create work directory
if (!Directory.Exists(workDir))
{
Directory.CreateDirectory(workDir);
}
// create temp file name
int i = 0;
string tmpfileprefix = workDir + "Rdp";
string tmpfilename = "";
do
{
i++;
tmpfilename = tmpfileprefix + i.ToString("D6");
} while (File.Exists(tmpfilename + ".rpt"));
caseFile = tmpfilename;
return caseFile;
}
}
}
Error:fil.Length = 'fil.Length'
'System.IO.FileNotFoundException'
引发了类型为无法找到文件的异常'C:\Users\sc\AppData\Local\Temp\\work\Rdp000001.rpt'
。
此外,if (fil.Exists)
返回 false。