C# 为下一次运行wpf应用程序保存数据字符串
我有一个功能,可以自动使用打印机名称并打印数据,而无需打开打印机对话框。 我希望能够将打印机名称保留为资源或文件,以防打印机不再存在或其名称已被修改,并为下一次运行应用程序保留更新的名称 我的问题是: 这样做的最佳实践是什么 我当前将打印机名称保留为常量字符串“printer\u name” 守则:C# 为下一次运行wpf应用程序保存数据字符串,c#,wpf,printing,C#,Wpf,Printing,我有一个功能,可以自动使用打印机名称并打印数据,而无需打开打印机对话框。 我希望能够将打印机名称保留为资源或文件,以防打印机不再存在或其名称已被修改,并为下一次运行应用程序保留更新的名称 我的问题是: 这样做的最佳实践是什么 我当前将打印机名称保留为常量字符串“printer\u name” 守则: private void print_DefaultNum_Print(object sender, EventArgs e) { bool isSent_ok = tru
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);
}
}
}
}
private void print\u DefaultNum\u print(对象发送方,事件参数e)
{
bool isSent_ok=真;
//依赖于设备的字符串,需要FormFeed吗?
字符串s=“!U1 getvar”+“设备.唯一\u id”;
//允许用户选择打印机。
PrintDialog pd=新建PrintDialog();
pd.PrinterSettings=新的PrinterSettings();
pd.PrinterSettings.PrinterName=打印机名称;
对于(int i=0;i
谢谢你的帮助 WPF在应用程序退出时触发事件。我将处理这个事件,在代码中,我将把任何持久化的数据写入一个文件。首先,在App.xaml中,开始在应用程序标记中键入“Exit=”。VisualStudio应该建议添加一个新的事件处理程序-单击该选项,以便它为您连接它。您的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>
要加载它,您可以使用相同的过程来处理启动事件-它连接的空方法的代码如下所示:
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...
}
}
看一看。谢谢,我用了这个解决方案
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...
}
}