如何关闭文件上的所有文件句柄并将其删除,该文件正在被复制并提供给打印机进行打印作业

本文关键字:文件 复制 打印机 打印 作业 何关闭 文件句柄 删除 | 更新日期: 2023-09-27 18:26:36

我有一个C#应用程序,我在其中向本地打印机发送一些字符串变量。所以我想做的是,我想从文件夹中删除一个特定的文件在我的打印命令之前复制到文件夹中。

是这样的。。将文件复制到//debug文件夹,打印完成后删除文件。我有点困惑如何理解我的印刷工作已经完成。以下是我的代码。

private void print_Click(object sender, EventArgs e)
    {
        string s = image_print() + Print_image();
        PrintFactory.sendTextToLPT1(s);
        /*string Filename = img_path.Text;
        MessageBox.Show("file", Filename);
        // if (Filename.ToCharArray().Intersect(Path.GetInvalidFileNameChars()).Any())
        //   return;
        File.Delete(Path.Combine(@"E:'Debug", Filename));*/
    }
private string image_print()
    {
        OpenFileDialog ofd = new OpenFileDialog();
        string path = "";
        string full_path = "";
        string filename_noext = "";
        ofd.InitialDirectory = @"C:'ZTOOLS'FONTS";
        ofd.Filter = "GRF files (*.grf)|*.grf";
        ofd.FilterIndex = 2;
        ofd.RestoreDirectory = true;
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            filename_noext = System.IO.Path.GetFileName(ofd.FileName);
            path = Path.GetFullPath(ofd.FileName);
            img_path.Text = filename_noext;
            //MessageBox.Show(filename_noext, "Filename");
            // MessageBox.Show(full_path, "path");
            //move file from location to debug
            string replacepath = @"E:'Debug";
            string fileName = System.IO.Path.GetFileName(path);
            string newpath = System.IO.Path.Combine(replacepath, fileName);
            if (!System.IO.File.Exists(filename_noext))
                System.IO.File.Copy(path, newpath);
        }
        StreamReader test2 = new StreamReader(img_path.Text);
        string s = test2.ReadToEnd();
        return s;
    }
    private string Print_image()
    {
        //passing some commands and returns its as a sting "S"
        return s;
    }

在我的独立类函数中,我有处理打印机的说明。所以下面是我的打印.cs 的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.IO;
namespace DVZebraPrint
{
public class Print
{
    public const short FILE_ATTRIBUTE_NORMAL = 0x80;
    public const short INVALID_HANDLE_VALUE = -1;
    public const uint GENERIC_READ = 0x80000000;
    public const uint GENERIC_WRITE = 0x40000000;
    public const uint CREATE_NEW = 1;
    public const uint CREATE_ALWAYS = 2;
    public const uint OPEN_EXISTING = 3;
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess,
        uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
        uint dwFlagsAndAttributes, IntPtr hTemplateFile);
    public static void sendTextToLPT1(String receiptText)
    {
        IntPtr ptr = CreateFile("LPT1", GENERIC_WRITE, 0,
                 IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
        /* Is bad handle? INVALID_HANDLE_VALUE */
        if (ptr.ToInt32() == -1)
        {
            /* ask the framework to marshall the win32 error code to an exception */
            Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
        }
        else
        {
            FileStream lpt = new FileStream(ptr, FileAccess.ReadWrite);
            Byte[] buffer = new Byte[2048];
            //Check to see if your printer support ASCII encoding or Unicode.
            //If unicode is supported, use the following:
            //buffer = System.Text.Encoding.Unicode.GetBytes(Temp);
            buffer = System.Text.Encoding.ASCII.GetBytes(receiptText);
            lpt.Write(buffer, 0, buffer.Length);
            lpt.Close();
        }
      }
    }
   }

如何关闭文件上的所有文件句柄并将其删除,该文件正在被复制并提供给打印机进行打印作业

您应该只从image_print()函数返回文件路径。则在打印字符串之后删除该文件。

private string image_print()
{
    ...Your code
    string newpath = string.Empty;
    if (!System.IO.File.Exists(filename_noext))
        System.IO.File.Copy(path, newpath);
    return newpath;
}

并在打印按钮点击事件中读取该文件中的字符串

private void print_Click(object sender, EventArgs e)
{
    string filePath = image_print();
    StreamReader test2 = new StreamReader(filePath);
    string s = test2.ReadToEnd();
    test2.Close();
    s += Print_image();
    PrintFactory.sendTextToLPT1(s);
    System.IO.File.Delete(filePath); //Now delete the file which you have copied in image_print() method.
}