【问题标题】:C# Create temp file, write to it, print it, then delete itC# 创建临时文件,写入,打印,然后删除
【发布时间】:2017-08-21 10:04:27
【问题描述】:

我正在尝试创建一个临时文件,对其进行写入、打印,然后将其删除。这是我的代码:

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 filePathReceipt = AppDomain.CurrentDomain.BaseDirectory + @"receipt.txt";

虽然这会产生异常:string filePathReceipt = 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);
        }

【问题讨论】:

  • 您可能需要等待进程完成才能尝试删除文件
  • 要创建文件,您需要使用FileMode.Create
  • Wait till a process ends的可能重复

标签: c#


【解决方案1】:

您混合了两个概念:FileStreamFile.WriteAllText
您打开一个文件流,然后使用 File.WriteAllText 尝试打开文件但未能做到这一点。

替换:

using (FileStream fs = new FileStream(filePathReceipt, FileMode.Open)) {
  File.WriteAllText(filePathReceipt, dataToBeWritten.ToString());
}

与任一:

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

// pay attention: it is FileMode.Create, not FileMode.Open
using (FileStream fs = new FileStream(filePathReceipt, FileMode.Create)) 
using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
{
  sw.Write(dataToBeWritten.ToString());
}

【讨论】:

  • 完美发现。文件被FileStream 使用,所以File.WriteAllText 方法抛出异常。
  • 我照你说的做了。相反,我得到了一个新异常,说“没有程序与该文件相关联”(粗略翻译)。
  • 很高兴,正在创建文件 :) 现在,关于打印:@Phrosen 你确定你使用正确的Verb 吗?请改用PrintPrintTo
  • @Phrosen 这是一个 Windows 错误。您的临时文件有一个扩展名,Windows 不知道使用动词“PRINT”的程序。
  • 我尝试了“Print”和“PrintTo”,都产生了与“PRINT”相同的异常。我没有打印机,所以也许这可能是个问题?我尝试使用相同的代码在我已经创建的文件上打印,但这似乎有效。 (我的意思是似乎可以工作:Windows 未检测到打印机,而是尝试将文件另存为 .pdf)
【解决方案2】:

除了Yeldar's answer...

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

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

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

另一种方法是使用Process'事件:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 2013-01-18
    相关资源
    最近更新 更多