So I am adding a help manual to my form and would like it to open a pdf in a separate window. Right now I am using this
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.FileName = #"Filepath";
process.Start();
}
But it obviously requires the full filepath. I want to include the pdf with the finished product and thus I won't know the full filepath on their system. I'm not sure if I can add this as a resource somehow? Sorry if I'm overlooking something simple.
Related
Basically i want the user to press a button and then the console will write all the appropriate lines for the user.
Here's the code i've written:
private void button6_Click(object sender, EventArgs e)
{
Process Cmd = new Process();
Cmd.StartInfo.FileName = #"C:\windows\system32\cmd.exe";
Cmd.Start();
StreamWriter sw = new StreamWriter(#"C:\windows\system32\cmd.exe");
{
sw.WriteLine = ("hello");
}
}
I tried StreamWriter but doesn't seem to be co-operating.
What you want can't be done, because you need to redirect StandardInput to send commands to cmd and when you do this, the cmd window will close. You still don't explain what exactly you want to archieve, but i can only think of two options:
Create a batch file with all the commands you want to execute and pass it as an argument to Process.
Redirect StandardInput and StandardOutput. This way you should take care of showing all the cmd messages. This is a bit messy and could lead you to deadlocks.
Edit
So at last, what you want is just run a console application with parameters. This is a sample using makecert:
Process ppp = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = #"C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\makecert.exe";
psi.Arguments = "-n 'CN=TempCA' -r -sv TempCA.pvk TempCA.cer";
ppp.StartInfo = psi;
ppp.Start();
you can do something like this
Process Cmd = new Process();
Cmd.StartInfo.FileName = #"makecert.exe"; // enter the correct path
cmd.StartInfo.Argument = "" // pass your aarguemnt
Cmd.Start();
my company is using Sharepoint and ADFS. For the use of WebDav however we need the users to get some tokens which they only get by opening the Internet Explorer and navigate to two sites. However they will lose the token every ~30 Minutes, so it has to be a recurring task.
So now my job is to:
Open 2 Websites with IE
Every 30 Minutes
Don't annoy the user
My current solution is "kinda" working but I am not really satisfied with it.
I have only VSExpress so no Services.
I have a minimized max opacity visible false Windows Form.
I have a GPO which copies an EXE file to the computer and then creates a timed job that starts it every 30 minutes after login. However it is not really working out, people still have trouble accessing webdav if they don't run the EXE manually. Also whenever the EXE is running the current application the user is working in loses focus which is kinda annoying when you are typing something and have to click back in.
My current code is looking like this:
private void Form1_Load(object sender, EventArgs e)
{
MainMethod();
}
private void MainMethod()
{
RegistryKey root = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Classes\InternetExplorer.ApplicationMedium\CLSID", false);
if (root!=null)
{
opensite();
Application.Exit();
}
}
private void opensite()
{
try
{
SHDocVw.InternetExplorer _ie1 = (SHDocVw.InternetExplorer)Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.ApplicationMedium"));
SHDocVw.InternetExplorer _ie2 = (SHDocVw.InternetExplorer)Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.ApplicationMedium"));
_ie1.Visible = false;
_ie2.Visible = false;
_ie1.Navigate("SITENAME1.html");
_ie2.Navigate("SITENAME2.html");
System.Threading.Thread.Sleep(10000);
_ie1.Quit();
_ie2.Quit();
}
catch(Exception e)
{
}
}
However, I feel there is a much more elegant way to do this. I heard the only way to open a hidden IE is via
(SHDocVw.InternetExplorer)Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.ApplicationMedium"));
But with this I rely on the registry key which not all clients have.
Can you help me open the IE in a reliable way and maybe have some tipps on how I should set the recurring task to just start every 30 minutes (because I think it is not doing it correctly atm).
Thank you all in advance.
EDIT:
Thanks to https://stackoverflow.com/users/5065008/daniel-waghorn
I now replaced the opensite bit with:
private void Form1_Load(object sender, EventArgs e)
{
MainMethod();
}
private void MainMethod()
{
openProc("SITE1.html");
openProc("SITE2.html");
Application.Exit();
}
private void openProc(string site)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
string ProgramFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
startInfo.FileName = ProgramFiles + #"\Internet Explorer\iexplore.exe";
startInfo.Arguments = "" + site + "";
startInfo.CreateNoWindow = true;
startInfo.ErrorDialog = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);
}
Thanks again!
You can use ProcessStartInfo to create a new instance of IE:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = ""C:\Program Files\Internet Explorer\iexplore.exe"";
startInfo.Arguments = "" + url + "";
startInfo.CreateNoWindow = true;
startInfo.ErrorDialog = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);
You could use Environment.SpecialFolder.ProgramFiles to get the user's Program Files directory path if you don't want to hard-code it.
I must point out that startInfo.WindowStyle will start Internet Explorer hidden although if at any point IE decides to alter that value for any reason it may show.
Ideally if you aren't tied to using Internet Explorer to get the tokens another alternative would be to use the above code but target cURL or something similar. With this it will run in the command line which you can guarantee not to show or steal focus with startInfo.CreateNoWindow.
I think you'll find your answer in one of these links:
Handle IE To filling a form c#
Opening a Hidden Internet Explorer Window without it getting Focus?
i am working in unity and i have a task of creating and then printing pdf from some of the snapshots taken through cameras in unity.
On windows after creating the pdf that can easily be done by calling the ShellExecute function and passing print as the parameter or using a function posted on stackoverflow i.e:
private void SendToPrinter()
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = #"c:\output.pdf";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.CloseMainWindow())
p.Kill();
}
, but i have no clue at all how would i be able to achieve the same for the OSX build?
Any help will be really appreciated.
You need to look at Apple's "Printing Programming Guide for Mac". (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Printing/osxp_aboutprinting/osxp_aboutprt.html)
To print a PDF page, you need a CGContextRef. In your views drawRect: method, you can get the the correct graphics context like this:
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
The method you use to draw a PDF page into a context is CGContextDrawPDFPage(context,page);
The easiest way to open a PDF document is to use CGPDFDocumentCreateWithURL
You get the pages using CGPDFDocumentGetPage.
I want to open IE (it doesn't matter the web site it will open with), but I found out that using this code:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "http://www.google.com";
proc.Start();
will start the browser as a current user, but with this code:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\Program Files\\Internet Explorer\\iexplore.exe";
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
it wouldn't, why is that? what is the reason?
Wrap the code in a try catch and take a look at the exception being raised, that should help find out why it is not working.
I have requirement to run a set of *.sql files using batch file like this.
private void btn_Execute_Click(object sender, EventArgs e)
{
try {
//Creating A batch file to execute the scripts using SQLPLUS....
FileInfo fi5 = new FileInfo("c:/EMPSCRIPTS/execute.bat");
StreamWriter sw2 = fi5.CreateText();
sw2.WriteLine("#Echo Off \r \nsqlplus scotte/tiger#emp #\"c:/EMPSCRIPTS/RUNALL.sql\" \r \nEXIT ");
sw2.Close();
System.Diagnostics.Process proc; // Declare New Process
proc = System.Diagnostics.Process.Start(ConfigurationManager.AppSettings["BatFilePath"].ToString()); // run test.bat from command line.
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = "/c" + ConfigurationManager.AppSettings["BatFilePath"].ToString();
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
proc.Close();
}
catch (Exception ex) {
MessageBox.Show("Insertion Completed");
}
}
But I want to stop some files being executed unnecessarily. I found the option for passing the parameters. I want to give the parameters to the files staticly. Based on the users input that parameter has to execute. Could any one help me?
Thanks in Advance.
You're changing the values of StartInfo after the process has been started, which has no effect on the process. See the "Remarks" section here for more information.
Create a new instance of ProcessStartInfo, set it up with what you need, then pass it into the overload of Start that takes an instance of this type.
In addition, once you change your code around, you can probably skip writing the command line to the batch file. Your executable filename is sqlplus and your arguments are scotte/tiger#emp #\"c:/EMPSCRIPTS/RUNALL.sql\"
You're misusing Process.Start.
You need to create a new ProcessStartInfo object, set all of its properties, then pass it to Process.Start.
Modifying the StartInfo of an already-running process, as your code is doing, has no effect.