My users can attach documents to various entities in the application. Of course, if user A attaches a .TIFF file, user B may not have a viewer for that type of file.
So I'd like to be able to bring up this dialog:
alt text http://www.angryhacker.com/toys/cannotopen.png
My application is C# with VS2005.
Currently I do Process.Start and pass in the file name. If no association is found, it throws an exception.
Process pr = new Process();
pr.StartInfo.FileName = fileTempPath;
pr.StartInfo.ErrorDialog = true; // important
pr.Start();
This should do it:
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "rundll32.exe";
p.StartInfo.Arguments = "shell32.dll,OpenAs_RunDLL " + yourFileFullnameHere;
p.Start();
Related
I am trying to run the process with different user. When I run normal "notepad.exe" it works fine. But when I change the file to any other executable with full path(C:\\Program Files\\Microsoft Office\\Office15\\Excel.exe) or (C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\AcroRd32.exe) it doesn't work. Instead gives errors like attached in pic.
Any suggestions...??
static void Main(string[] args)
{
SecureString securePwd = new SecureString();
string password = "P#ssw0rd";
SecureString sec_pass = new SecureString();
Array.ForEach(password.ToArray(), sec_pass.AppendChar);
sec_pass.MakeReadOnly();
Process p = new Process();
ProcessStartInfo ps = new ProcessStartInfo();
p.StartInfo.FileName = "notepad.exe";
p.StartInfo.Arguments = "C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\welcome.pdf";
p.StartInfo.WorkingDirectory = "C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\";
p.StartInfo.ErrorDialog = true;
p.StartInfo.EnvironmentVariables.Add("TempPath", "C:\\Temp");
p.StartInfo.Domain = "testdom";
p.StartInfo.UserName = "testuser";
p.StartInfo.Password = sec_pass;
p.StartInfo.RedirectStandardError = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
StreamReader myStreamReader = p.StandardOutput;
// Read the standard error of net.exe and write it on to console.
Console.WriteLine(myStreamReader.ReadLine());
p.Close();
}
Notepad doesn't store any user-specific settings. I'm certain all of the Office products do, and it wouldn't surprise me if Acrobat does too.
So, first thing to fix is to make sure that your ProcessStartInfo sets LoadUserProfile to true. That may be sufficient.
However, sometimes applications also act quite differently when run for the first time versus any subsequent launches, so I'd also make sure that you have, at least once, launched each of these applications as the intended target user, whilst you're actually logged onto the machine as that user (versus just launching a single process as that user).
In your code example your trying to open a pdf document in notepad.
Just checking, what happens when you change the file name to the adobe exe (you may need to add the path to the exe) instead of notepad.exe
I try to print file for example C:/exmaple.docx but I need to specify for it printer and tray which I get from print dialog. I do not now how to set tray (paper source) as argument. Setting printer as argument works. This is my code:
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
info.Arguments = "\"" + somePrinterName + "\"";
info.Verb = "C:\\example.docx";
info.FileName = "C:\\example.docx";
info.UseShellExecute = true;
info.CreateNoWindow = true;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = info;
p.Start();
I don't believe there are printer tray options in the System.Diagnostics namespace, but for Word documents you can print from a specific tray by using the Word Interop library (Microsoft.Office.Interop.Word).
That'd let you do something like
wordDocument.PageSetup.FirstPageTray = Word.WdPaperTray.wdPrinterUpperBin;
wordDocument.PageSetup.OtherPagesTray = Word.WdPaperTray.wdPrinterLowerBin;
There's a more fleshed-out example here (that developer has tray selection working, but is struggling with duplex printing).
See also:
MSDN Reference for WdPaperTray enum
MSDN Social: Printing Options C#
I am creating a new process on my Sharepoint web application. I've run the same command as below and works on my OS but not on my web app. I wanted to know why and if this is even possible. Here's code that creates the process.
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"report.txt");
Process proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "msinfo32.exe";
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.Arguments = "/report " + filePath;
proc.Start();
proc.WaitForExit();
proc.Close();
It creates a file with every filed as Can't collect information, example:
Can't Collect Information
[Hardware Resources]
[Conflicts/Sharing]
Can't Collect Information
[DMA]
Am I doing it wrong, are there settings to enable on sharepoint in order to run msinfo32?
WMI is enabled on my OS.
Found the answer. Sharepoint requires an elevated security state to run this command. Wrapping the code in this helped run msinfo32.exe. Thanks to Amal Hashim and Method Man for recommending this
SPSecurity.RunWithElevatedPrivileges(delegate()
{
//Your code goes here
});
I went to the directory where my debug folder is running a console app on my side
here is what I did and it work..
change the name of your Report.txt to out.log open the out.log file and just type anything in it.. save it and close it .. and run the following code below and you will se the SystemInformation window pop up. add this in the Page_Load event to test it
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "out.log");
Process proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "msinfo32.exe";
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.Arguments = "/out " + filePath;
proc.Start();
proc.WaitForExit();
proc.Close();
I just tested this from my Page_Load in my web page and it works like a charm.. what you need to add to the header of the webpage that you are launching it from is the following
using System.Diagnostics;
using System.IO;
I am creating a Windows Form Application and I want the user to be able to open the log file on request, after selecting the option on a menu strip.
I can open the file within notepad but the most recent entries will be at the end of the file. How would I make the application start at the end of the file to save the user a job?
My Current Code:
public static void OpenCurrentLog()
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = Environment.GetEnvironmentVariable("windir").ToString() + "\\system32\\notepad.exe";
startInfo.Arguments = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +"\\" appName + "\\" + "\\LogFiles\\LogFile.log";
startInfo.WindowStyle = ProcessWindowStyle.Normal;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
Any Help would be appreciated. I am relatively new to c#.
If you send CTRL (^) - END ({END}) through it will move to the bottom of the notepad file
SendKeys.Send("^{END}");
I'm trying to grab snapshots of my own website using phantomjs - basically, this is to create a "preview image" of user-submitted content.
I've installed phantomjs on the server and have confirmed that running it from the command line against the appropriate pages works fine. However, when I try running it from the website, it does not appear to do anything. I have confirmed that the code is being called, that phantom is actually running (I've monitored the processes, and can see it appear in the process list when I call it) - however, no image is being generated.
I'm not sure where I should be looking to figure out why it won't create the images - any suggestions? The relevant code block is below:
string arguments = "/c rasterize.js http://www.mysite.com/viewcontent.aspx?id=123";
string imagefilename = #"C:\inetpub\vhosts\mysite.com\httpdocs\Uploads\img123.png";
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = false;
p.StartInfo.FileName = #"C:\phantomjs.exe";
p.StartInfo.Arguments = arguments + " " + imagefilename;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
I check the errors that phantomjs throws during its process.
You can read them from Process.StandardError.
var startInfo = new ProcessStartInfo();
//some other parameters here
...
startInfo.RedirectStandardError = true;
var p = new Process();
p.StartInfo = startInfo;
p.Start();
p.WaitForExit(timeToExit);
//Read the Error:
string error = p.StandardError.ReadToEnd();
It will give you an idea of what happened
The easiest way for executing phantomjs from C# code is using wrapper like NReco.PhantomJS. The following example illustrates how to use it for rasterize.js:
var phantomJS = new PhantomJS();
phantomJS.Run( "rasterize.js", new[] { "https://www.google.com", outFile} );
Wrapper API has events for stdout and stderr; also it can provide input from C# Stream and read stdout result into C# stream.