C# 创建临时文件,写入,打印,然后删除

C# 创建临时文件,写入,打印,然后删除,c#,C#,我正在尝试创建一个临时文件,写入它,打印它,然后删除它。这是我的密码: string filePathReceipt = Path.GetTempFileName(); try { using (FileStream fs = new FileStream(filePathReceipt, FileMode.Open)) { File.WriteAllText(filePathReceipt, dataToBeWritten.ToString()); } } c

我正在尝试创建一个临时文件,写入它,打印它,然后删除它。这是我的密码:

string filePathReceipt = Path.GetTempFileName();

try {
    using (FileStream fs = new FileStream(filePathReceipt, FileMode.Open)) {
        File.WriteAllText(filePathReceipt, dataToBeWritten.ToString());
    }
}
catch (Exception ex) {
    MessageBox.Show(ex.Message);
}

//Printing receipt: start
ProcessStartInfo psi = new ProcessStartInfo(filePathReceipt);
psi.Verb = "PRINT";

try {
    Process.Start(psi);
}
catch (Exception ex) {
    MessageBox.Show(ex.Message);
}
//Printing receipt: end

if (File.Exists(filePathReceipt)) {
    //Delete the file after it has been printed
    File.Delete(filePathReceipt);
}
我收到的异常消息是:

无法写入,因为它已被其他进程使用

编辑#2: 我发现这是有效的:
string filepathreivement=AppDomain.CurrentDomain.BaseDirectory+@“received.txt”

而这会生成一个异常:
string filepathReceive=Path.GetTempFileName()

完整的当前代码:

        //string filePathReceipt = AppDomain.CurrentDomain.BaseDirectory + @"receipt.txt";
        string filePathReceipt = Path.GetTempFileName();

        File.WriteAllText(filePathReceipt, dataToBeWritten.ToString());

        ProcessStartInfo psi = new ProcessStartInfo(filePathReceipt);
        psi.Verb = "PRINT";

        try {
            using (Process p = Process.Start(psi))
                p.WaitForExit();
        }
        catch (Exception ex) {
            MessageBox.Show(ex.Message);
        }


        if (File.Exists(filePathReceipt)) {
            //Delete the file after it has been printed
            File.Delete(filePathReceipt);
        }

您混合了两个概念:
FileStream
File.writealText

您打开一个文件流,然后使用
file.WriteAllText
尝试打开文件,但无法打开

替换:

using (FileStream fs = new FileStream(filePathReceipt, FileMode.Open)) {
  File.WriteAllText(filePathReceipt, dataToBeWritten.ToString());
}
与以下任何一项:

File.WriteAllText(filePathReceipt, dataToBeWritten.ToString());

除了

您需要等待打印过程完成。您无法在启动该进程后的毫秒内删除该文件

等待该过程的直接方法是:

try {
    using (Process p = Process.Start(psi))
        p.WaitForExit();
}
catch (Exception ex) {
    MessageBox.Show(ex.Message);
}

另一种方法是使用
过程
”事件:

using(Process p = new Process())
{
    p.StartInfo = psi;
    p.EnableRaisingEvents = true;
    p.HasExited += OnProcessExited;
}

并在
OnProcessExited
事件处理程序中处理该文件。

您可能需要等待进程完成。如果尝试删除该文件,则需要使用
文件模式。创建
。该文件可能重复。File被
FileStream
使用,因此
File.WriteAllText
方法引发异常。我按照您告诉我的做了。我得到了一个新的例外,说了一些关于“没有任何程序与该文件关联用于该目的”(粗略翻译)。幸运的是,正在创建文件:)现在,关于打印:@Phrosen你确定你使用了正确的
动词吗?请改为尝试
Print
PrintTo
。@Phrosen这是windows错误。您的临时文件有一个扩展名,windows不知道有哪个程序使用动词“PRINT”作为扩展名。我尝试了“PRINT”和“PrintTo”,两者都生成了与“PRINT”相同的异常。我没有打印机,所以这可能是个问题?我试着用同样的代码在我已经创建的文件上打印,但这似乎有效。(我的意思是:Windows没有检测到打印机,而是试图将文件保存为.pdf)
using(Process p = new Process())
{
    p.StartInfo = psi;
    p.EnableRaisingEvents = true;
    p.HasExited += OnProcessExited;
}