System.Diagnostics.Process.Start() doesn't work in the web server - c#

I have a C# asp.net web page which reads PDF file from Mysql database (which stores as longblob format) and open it. It works in my Localhost; the page reads the file from database and open with acrobat reader but it doesn't work in the testing server after i deploy the page. The acrobat reader doesn't open and i don't see acroRd32.exe in the taskmgr manager. I feel it is permission issue because i use process.start() which may not allow in the server but i dont see error messages. If there are permissions needs to be done in server; can anyone kindly points me the direction?
Thank You.
Here are my code:
MySqlDataReader Reader = null;
connection.Open();
MySqlCommand command = new MySqlCommand("Select Image, File_Type, File_Name from table where ImageID = " + ImageID, connection);
Reader = command.ExecuteReader();
if (Reader.Read())
{
byte[] buffer = (byte[])Reader["Image"];
System.IO.MemoryStream stream1 = new System.IO.MemoryStream(buffer, true);
stream1.Write(buffer, 0, buffer.Length);
String fileName = Reader["File_Name"].ToString();
String dirName = "C:\\thefolder\\";
if (!Directory.Exists(dirName))
{
// if not then create
Directory.CreateDirectory(dirName);
}
if (File.Exists(dirName+fileName))
File.Delete(dirName + fileName);
Directory.CreateDirectory(Path.GetDirectoryName(Reader["File_Name"].ToString()));
using (Stream file = File.Create(dirName + fileName))
{
file.Write(buffer, 0, buffer.Length);
}
Process process = new Process();
process.StartInfo.FileName = "AcroRd32.exe";
process.Start();
}
Thanks for your help, i am able to send pdf content via response. Here is the code
//Process process = new Process();
//process.StartInfo.FileName = "AcroRd32.exe";
//process.Start();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment");
Response.TransmitFile(dirName + fileName);
Response.End();

Your C# code runs on the server.
Therefore, Process.Start starts a process on the server, not your computer.
It is fundamentally impossible for you to start a process directly on the client.
However, if you serve the PDF in the HTTP response (with the correct Content-Type), the browser will open it in a PDF viewer.

you don't have permissions to do that from the ASP.Net worker process.
you need impersonation:
ASP.NET Impersonation
How To: Use Impersonation and Delegation in ASP.NET 2.0
Hadn't read the question thoroughly...
If you won't to start a process on the server, you can use impersonation.
Otherwise you should serve this file from the IIS - to allow the user to download it.
Serving Dynamic Content with HTTP Handlers
Or if you are useing ASP.NET.MVC:
ASP.NET MVC - How do I code for PDF downloads?

This code works today (2022) in IIS 10, Windows Server 2019:
string strFileExePath = ConfigurationManager.AppSettings["Audio:ffmpeg.Directory"].ToString();
string strFileExe = ConfigurationManager.AppSettings["Audio:ffmpeg.Directory"].ToString() + "ffmpeg.exe";
// http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.useshellexecute.aspx
// http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx
// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(strFileExe);
psi.UseShellExecute = false;
psi.RedirectStandardOutput = false;
psi.RedirectStandardInput = true; // avoid hang, later close this...
psi.RedirectStandardError = false;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.CreateNoWindow = false; // If the UseShellExecute property is true or the UserName and Password properties are not null, the CreateNoWindow property value is ignored and a new window is created.
psi.ErrorDialog = false;
psi.WorkingDirectory = strFileExePath;
// argumentos
psi.Arguments = $"-y -i \"{fileWithPath}\" -aq 8 -ac 1 -f s16le -ar 8000 -acodec pcm_s16le -vol 500 \"{fileOutputWithPath}\"";
// Start the process
using (Process process = Process.Start(psi))
{
// http://csharptest.net/321/how-to-use-systemdiagnosticsprocess-correctly/
process.StandardInput.Close(); // no wait for input, avoid hang
// process.WaitForExit(); can produce request timeout
}

Related

SEP (Symantec Endpoint Protection) for C#

I am using Doscan.exe of SEP to scan files before uploading but there is no provision provided by SEP to generate a separate log file for each scanned file, so that's why I am not able to make sure whether the file is ok or not for uploading.
If anyone has a way to check this thing then please let me know.
I am using version 14.2
Thanks.
ProcessStartInfo start = new ProcessStartInfo();
start.Arguments = " /ScanFile C:\\Users\\New Text Document (2).txt";
start.FileName = #"C:\Program Files (x86)\Symantec\Symantec Endpoint Protection\14.2.3332.1000.105\Bin\DoScan.exe";
start.WindowStyle = ProcessWindowStyle.Hidden;
start.CreateNoWindow = true;
using (Process proc = Process.Start(start))
{
proc.WaitForExit();
}

Printing html file from windows application is not working when we set default browser as chrome (other than IE)

I have to print html file when user clicks on print button and it is working fine (prompts print dialog) when I set default browser as IE.
If I change default browser to chrome or firefox other than IE, the code does not prompting print dialog instead it just opens html file in the browser. Can you please let me know what configuration I have missed in the below code?
string TempFile = #"D:\test.html";
ProcessStartInfo Params = new ProcessStartInfo();
Params.FileName = "iexplore.exe";
Params.Arguments = TempFile;
Params.UseShellExecute = false;
Params.Verb = "print";
Params.WindowStyle = ProcessWindowStyle.Hidden;
Params.CreateNoWindow = true;
Process.Start(Params);
Finally I got the solution for this issue. The below code works like a charm!!
using (Process exeProcess = new Process())
{
string TempFile = #"D:\test.html";
exeProcess.StartInfo.FileName = "rundll32";
exeProcess.StartInfo.Arguments = #"system32\mshtml.dll,PrintHTML """ + TempFile + #"""";
exeProcess.StartInfo.UseShellExecute = true;
exeProcess.Start();
}

Print Save as PDF

I have the below code. As per my knowledge it is converting and saving as pdf. Can anyone explain this code?
Process cnp = new Process();
cnp.StartInfo.FileName = "AcroRd64.exe";
cnp.StartInfo.Arguments = "/n /t c:/test.jpg Microsoft Office Document Image Writer";
Updates:
I created a sample console application to trigger print and it is not working
class Program
{
static void Main(string[] args)
{
try
{
Process p = new Process();
p.StartInfo.FileName = #"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe";
p.StartInfo.Verb = "Print";
p.StartInfo.Arguments = "/n /t c:/test.png " + "Microsoft Office Document Image Writer";
p.StartInfo.CreateNoWindow = false;
p.StartInfo.UseShellExecute = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadKey();
}
}
Process is used to start and stop processes on your system.
Seems pretty clear that your code attempts to start a process "AcroRd64.exe", which I would guess is the pdf-reader Adobe Acrobat Reader. Argumens are arguments to the process, so basically, this is the equivalent of writing the following in command line:
AcroRd64.exe /n /t c:/test.jpg Microsoft Office Document Image Writer
There's a little more info on this under this other SO question.
Your code might doesn't work because the single argument Microsoft Office Document Image Writer contains whitespace. Try:
cnp.StartInfo.Arguments =
"AcroRd64.exe /n /t c:/test.jpg \"Microsoft Office Document Image Writer\"";

Text content in pdf is not converted using pdf2swf

I am running c# application in service mode. And i am using pdf2swf tool to convert odf to swf format. Images saved in pdf is converting. But if any test adding to pdf is not getting converted in service mode.
But when run as UI mode(Consoleapplication.exe) then everything is getting converted.
string inputFileName = this.Filename;
string outputFileName = inputFileName.Replace("pdf", "swf");
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0} -o {1}", inputFileName, outputFileName);
string executingDirPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", "");
string dataDirectoryPath = Path.Combine(executingDirPath, "pdf2swf.exe");
ProcessStartInfo psi = new ProcessStartInfo(dataDirectoryPath, sb.ToString());
psi.UseShellExecute = false;
System.Diagnostics.Process pdf2swf = new System.Diagnostics.Process();
pdf2swf.StartInfo = psi;
pdf2swf.Start();
pdf2swf.WaitForExit();
pdf2swf.Close();
pdf2swf.Dispose();
Regards
Sangeetha
Direct using process to start pdf2swf.ext maybe had some privilege problems.I used another way to solve this problem,write a batch file,then running the batch file by process.
Batch file sample:
c:
cd C:\Program Files (x86)\SWFTools\
pdf2swf.exe -f -T 9 -t "%1" -o "%2"
Code in program:
Process p = new Process();
string path = basePath + "/plugin/ConvertToSwf.bat";//batch file path
ProcessStartInfo pi = new ProcessStartInfo(path, filePath + " " + swfPath);//passing the file path and converted file path to batch file
pi.UseShellExecute = false;
pi.RedirectStandardOutput = true;
p.StartInfo = pi;
p.Start();
p.WaitForExit();
I faced a similar problem recently. I solved the issue by adding a separate console application(Consoleapplication.exe) with administrative-rights that runs on my server without shell.
Also, try to upgrade to the newest version of pdf2swf.
FYI. I recently had this problem (thought it was fonts not being embedded but actually was missing all text in converted swf). What fixed it for me was to set:
pi.UseShellExecute = false;
AND set the working directory;
pi.WorkingDirectory = "C:\windows\temp"; // path where read & write is

Auto FTP from a Directory

I want to monitor a directory and FTP any files that are place there to an FTP location. does anyone know how to do this in c#?
Thanks
EDIT: Anyone know of a good client that can monitor a directory and FTP and files placed in it?
I combination of the System.IO.FileSystemWatcher and System.Net.FtpWebRequest/FtpWebResponse classes.
We need more information to be more specific.
When used in conjunction with the FileSystemWatcher, this code is a quick and dirty way to upload a file to a server.
public static void Upload(string ftpServer, string directory, string file)
{
//ftp command will be sketchy without this
Environment.CurrentDirectory = directory;
//create a batch file for the ftp command
string commands = "\n\nput " + file + "\nquit\n";
StreamWriter sw = new StreamWriter("f.cmd");
sw.WriteLine(commands);
sw.Close();
//start the ftp command with the generated script file
ProcessStartInfo psi = new ProcessStartInfo("ftp");
psi.Arguments = "-s:f.cmd " + ftpServer;
Process p = new Process();
p.StartInfo = psi;
p.Start();
p.WaitForExit();
File.Delete(file);
File.Delete("f.cmd");
}

Categories

Resources