保存下一次运行wpf应用程序的数据字符串

时间:2017-01-11 16:35:40

标签: c# wpf printing

我有一个自动使用打印机名称并在不打开打印机对话框的情况下打印数据的功能。 我希望能够将打印机名称保留为A资源或A文件以防打印机不再存在或者其名称已被修改,并保留更新的名称以便下次运行应用程序

我的问题是: 这样做的最佳做法是什么?

我目前将打印机名称保留为常量字符串'PRINTER_NAME' 代码:

 private void print_DefaultNum_Print(object sender, EventArgs e)
    {
        bool isSent_ok = true;
        // device-dependent string, need a FormFeed?
        string s = "!U1 getvar " + "device.unique_id";
        // Allow the user to select a printer.
        PrintDialog pd = new PrintDialog();
        pd.PrinterSettings = new PrinterSettings();
        pd.PrinterSettings.PrinterName = PRINTER_NAME;

        for (int i = 0; i < DEFAULT_NUM_COPIES && isSent_ok; i++)// send # of copies
        {
            isSent_ok = RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, deviceName);
            if (isSent_ok == false)
            {

                if (DialogResult.OK == pd.ShowDialog(this))
                {

                    // Send a printer-specific to the printer.
                    for (int j = 0; j < pd.PrinterSettings.Copies; j++)// send # of copies
                        RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, deviceName);
                }
            }
        }
    }

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

WPF在应用程序退出时触发事件。我会处理这个事件,在那个代码中,我会将任何持久化数据写入文件。首先,在App.xaml中,开始在Application标记中键入“Exit =”。 Visual Studio应该建议添加一个新的事件处理程序 - 单击该选项,以便为您连接它。您的app.xaml将类似于:

<Application x:Class="MyWpfApplication.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:MyWpfApplication.ViewModel"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:ignore="http://www.galasoft.ch/ignore"
             StartupUri="MainWindow.xaml"
             Exit="Application_Exit"
             mc:Ignorable="d ignore">


</Application>

现在转到app.xaml.cs文件,您应该看到一个名为Application_Exit的空方法。添加代码以将字符串保存到文件中:

private void Application_Exit(object sender, ExitEventArgs e)
{
    string applicationPath = Path.GetFullPath(System.AppDomain.CurrentDomain.BaseDirectory); // the directory that your program is installed in
    string saveFilePath = Path.Combine(applicationPath, "printini.txt"); // add a file name to this path.  This is your full file path.
    File.WriteAllText(saveFilePath, PRINTER_NAME);
}

要加载它,您可以使用相同的过程来处理Startup事件 - 它连接的空方法的代码如下所示:

private void Application_Startup(object sender, StartupEventArgs e)
{
    string applicationPath = Path.GetFullPath(System.AppDomain.CurrentDomain.BaseDirectory); // the directory that your program is installed in
    string saveFilePath = Path.Combine(applicationPath, "printini.txt"); // add a file name to this path.  This is your full file path.

    if (File.Exists(saveFilePath))
    {
        PRINTER_NAME = File.ReadAllText(saveFilePath);
    }
    else
    {
        // prompt the user for a printer...
    }
}