how do I accomplish secure printing in C#? - c#

I need to print documents to the printer such that the user is required to enter a PIN before the document is printed out.
In my research thus far the only method I have seen accomplish this is to print the document to a PRN file and then edit the file in binar looking for
byte[] searchfor = System.Text.Encoding.ASCII.GetBytes(#"#PJL SET HOLD=OFF" + "\n" + "#PJL SET USERNAME=\"SYSTEM\"");
and replace it with
byte[] new_lines = System.Text.Encoding.ASCII.GetBytes(#"#PJL SET HOLD=ON" + Environment.NewLine + #"#PJL SET HOLDTYPE=PRIVATE" + Environment.NewLine + "#PJL SET HOLDKEY=\"" + User_Pin + "\"" + Environment.NewLine + "#PJL SET USERNAME=\"" + User_Name + "\"");
This seems a little silly to me. I have not found anything within the PrintDocument or PrinterSettings that seems to help.
While this is a solution, we print out large image files and if we then send this directly to the printer via a copy command then the RAM on the printer isn't sufficient to hold the entire document. I would like to have a more elegant way to steam to the document through the print queue so that the queue spool it to the printer as it can handle it.
Any help would be greatly appreciated.
Thank you,
Richard

That's because it's a printer feature, optionally exposed via the driver, and not something Windows can do.

Related

FFMpegCore not generating an output when using snapshot in C#

I'm new to using FFMpeg, I've tried using FFMpeg in c# many different ways, and every time I get no output. I found the FFMpegCore package and installed it, the FFProbe stuff works but the Snapshot isn't doing anything. I'm getting neither errors nor any output files. I just want to get a single frame from a selected video.
FFMpeg.Snapshot(frPath.SelectedPath + "/" + input, frPath.SelectedPath + '/' + input.Replace(".mp4", ".jpeg"), new Size(400, 400), TimeSpan.FromMinutes(1));
input is just the file name selected from a file dialog.
Am I doing something wrong, is there something that I have to install other than FFMpeg, or does FFMpeg.snapshot not work the way I think it does?
I was having the same problem when using TimeSpan.FromMinutes(1). I changed it to FromMilliseconds(1), and now it works!
FFMpeg.Snapshot(frPath.SelectedPath + "/" + input, frPath.SelectedPath + '/' + input.Replace(".mp4", ".jpeg"), new Size(400, 400), TimeSpan.FromMilliseconds(1));

Set up Scheuled Task to create Excel File and send email with attachment in C# Console application

I had a problem where a scheduled task would not send out an email with an attachment in a C# console application. It took me a long time to figure out the solution (it is working now), so I wanted to share what the two issues were so others can find the solution easily.
There were two problems:
1) When saving the file to the directory I could not use a relative directory like:
DirectoryInfo currentDir = new DirectoryInfo(Directory.GetCurrentDirectory());
attachmentPath = currentDir.Parent.Parent.FullName + #"\Reports\" + startDate.ToString("yyyy.MM.dd") + " - " + endDate.ToString("yyyy.MM.dd") + ".xlsx";
I had to explicitly set the directory like:
attachmentPath = #"E:\reports\Excel Automated Reports\WebEx Daily Report\ExcelReportAutomation\Reports\" + startDate.ToString("yyyy.MM.dd") + " - " + endDate.ToString("yyyy.MM.dd") + ".xlsx";
After, I did that the file was being created and saved but still was not emailing it out. If I sent out an email without an attachment then the email would go out. The way to fix this issue is by setting the Start in (optional) option in the edit action for the scheduled task.
I repeat this is not a question but I am trying to lay out a solution for people who come across the same issue.

Run application | app say error

I try to run my game in my game launcher but it says:
Memory access violation
Code
String name = GetValue(File.ReadAllLines(Co), "lib-name" + DubDown);
System.Diagnostics.Process.Start(path + "\\Games\\" + name + "\\" + GetValue(File.ReadAllLines(Co), "lib-file" + DubDown));
String name = GetValue(File.ReadAllLines(Co), "lib-name" + DubDown);
Why are you reading all lines of a file, but the file name shown below is used?
Does the file contain the names of all the files you want to open?? I think you'll need a loop to do that
String name....
shown above is included in the code below twice when written here
... + name +....
and here (as the value of name)
....GetValue(File.ReadAllLines(Co), "lib-file" + DubDown));
Try writing this like this.
System.Diagnostics.Process.Start(path + "\\Games\\" + name);
Unfortunately, as noted above, you'll need to iterate through the contents of the file prior to getting the names from inside the file, as your first line of code portrays. Sort of.
I think you should post more of the code for better assistance.

Writing a CSV log file

I have a Winforms program that needs to log data points into a .CSV file. It's fairly simple, date/time and a double (the data), and go to the next line.
Here's what I have so far (not working, I get an error saying the file is busy/already open - however, it's empty)
if (!Directory.Exists(SavePath.Text + "\\LOG"))
Directory.CreateDirectory(SavePath.Text + "\\LOG");
string LogFileName = SavePath.Text + "\\LOG\\Seeing-Log-" + TimeNow.ToString("yyyy-MM-dd") + ".csv";
if (!File.Exists(LogFileName))
File.Create(LogFileName);
string LogString = TimeNow.ToString("yyyy-MM-dd-_HH-mm-ss") + "," + FWHM_Value.ToString("F:");
File.AppendAllText(LogFileName, LogString + Environment.NewLine);
It's that last line that generates the error.
Any idea what I am doing wrong?
thanks
Steve
File.Create returns an open FileStream to the file that's just been created. Either change your code to work with FileStream in both the non-existent and existent file cases, or just close the file after creating it:
if (!File.Exists(LogFileName))
File.Create(LogFileName).Close();
But, of course, if you check the documentation for AppendAllText:
Appends the specified stringto the file, creating the file if it does not already exist.
You'll realise that the above two lines are completely redundant anyway and can be removed:
if (!Directory.Exists(SavePath.Text + "\\LOG"))
Directory.CreateDirectory(SavePath.Text + "\\LOG");
string LogString = TimeNow.ToString("yyyy-MM-dd-_HH-mm-ss") + "," + FWHM_Value.ToString("F:");
File.AppendAllText(LogFileName, LogString + Environment.NewLine);
You can even use the free looging tools. Here is one 'log4net'
You can also write the csv file using this. I am assuming currently you are not using logging tool. it will work for you without any code for implementation .
http://element533.blogspot.in/2010/05/writing-to-csv-using-log4net.html
Have a great day!!
Replace
File.Create(LogFileName);
with
File.Create(LogFileName).Close();
see this to create empty file.
The file is locked when you create it. Just update your code to this:
File.Create(LogFileName).Close();

Zip File Entry has custom file extension. C# claims it cannot open

Solution Found.
Thanks to everyone helping me, I found out what the root problem was. The .trl file had nothing to do with it. It was the path being created wrong. I was doing "TRLR" + Path, when it should have been "TRLR" + fileName. This was a stupid error on my part, and I apologize for wasting your time, but I appreciate the help!
I have a zip file given to us by a 3rd party. In this zip files are custom files. These are just text files with a different extension, which I assume is just to frustrate me.
I'm trying to open this files in my C# application, but it keeps throwing the error that the format is not supported.
Since these are just text files, I feel there must be some way for this to happen.
If anyone has any ideas, please let me know.
Code:
using (ZipArchive archive = ZipFile.OpenRead(_trailerName))
{
ZipArchiveEntry entry = archive.GetEntry(tableChanged + ".trl");
Stream ms = entry.Open(); //Here is what's causing the issue.
StreamReader reader = new StreamReader(ms);
string allLinesRead = reader.ReadToEnd();
string[] everyCell = allLinesRead.Split('|');
int numRecords = Convert.ToInt32(everyCell[1]);
int numChanged = getRecordNum(tableChanged);
Console.Write(numRecords + "/" + numChanged + " - " + tableChanged);
if (numChanged != numRecords)
{
_errorMessage += "Records updated do not match trailer. (" + numRecords + "/" + numChanged + ") Please check database. Table affected: " + tableChanged + Environment.NewLine;
}
}
Error:
The given path's format is not supported.
I know this is specific, but I need advice on what steps I can take to resolve this.
Thanks.
The native zip functionality of .NET is frequently lacking in terms of the ability to handle and modify zip files created by applications other than the windows zip tool. While the "zip" file is standardized, you still see a decent amount of variation on file headers and attributes.
I would suggest you look into DotNetZip (Ionic), which is a third party library that has very robust capabilities in terms of creating and opening zip files. I've found it to be much more forgiving and capable than the basic functionality that .NET gives you, and the code to open a zip is extremely similar to what you have.

Categories

Resources