2017-01-11 7 views
0

私は、プリンタ名を自動的に使用し、プリンタダイアログを開かずにデータを印刷する機能を持っています。 プリンターがもう存在しない、または名前が変更されている場合に、プリンター名をAリソースまたはAファイルとして保持したい場合は、次回のアプリケーション実行時に更新された名前を保持するようにしてくださいwpfアプリケーションの次の実行のためのデータ文字列を保存

質問は: これを実行するベストプラクティスは何ですか?

私は現在のconst文字列「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); 
       } 
      } 
     } 
    } 

感謝を!

+0

[アプリケーションの設定とユーザーの設定](https://msdn.microsoft.com/en-us/library/bb397750(v=vs.110).aspx)を参照してください。 – Clemens

答えて

1

WPFは、アプリケーションが終了するとイベントを発生させます。私はこのイベントを処理し、そのコードでは、永続化されたデータをファイルに書き出します。まず、App.xamlで、アプリケーションタグに「Exit =」と入力します。 Visual Studioでは、新しいイベントハンドラを追加することをお勧めします。今すぐあなたのapp.xaml.csファイルに移動し、あなたがApplication_Exitと呼ばれる空のメソッドが表示されるはずです

<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は次のようになります。あなたはStartupイベントを処理するために同じプロセスを使用することができ、それをロードするには

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); 
} 

- アップ空のメソッドのIT配線のためのコードは次のようになります:文字列をファイルに保存するためのコードを追加します。

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... 
    } 
} 
+0

ありがとう、私はこのソリューションを使用した –