I routinely use Process.Start(file); to open a file (PDF, Work or Excel file typically) using the default application. However, in this case I need to open a file that is on a server and using different credentials. I've tried every variation of Process that I can think of and nothing seems to work.
string file = folderPath + dlg.listBox1.Items[dlg.listBox1.SelectedIndex].ToString();
Process.Start(file, "xxxx", GetSecureString("xx11xx"), "MyCompany.com");
with this I get "The directory name is invalid".
Process proc = new Process
{
EnableRaisingEvents = false,
StartInfo =
{
UseShellExecute = false,
Verb = "open",
UserName = "xxxx",
Password = GetSecureString("xx11xx"),
Domain = "MyCompany.com",
FileName = folderPath + dlg.listBox1.Items[dlg.listBox1.SelectedIndex].ToString(),
WorkingDirectory = folderPath,
Arguments = dlg.listBox1.Items[dlg.listBox1.SelectedIndex].ToString(),
}
};
proc.Start();
With this, I get is "The specified executable is not a valid Win32 application."
If I drop the folderPath from FileName; i get "The system cannot file the file specified".
By the way, I am using a class I found on StackOverflow to successfully access the folder on the server and copy files to it and list the files in it.
Any help would be appreciated.
Thanks.
Related
I have a C# project where I use the Process class to run R scripts:
public void RunRScriptFile(string file) {
StringBuilder eventLog = new StringBuilder();
StringBuilder errorLog = new StringBuilder();
var scriptRunProcess = new Process {
StartInfo = new ProcessStartInfo {
FileName = "Rscript",
Arguments = file,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = false
}
};
scriptRunProcess.Start();
while (!scriptRunProcess.StandardOutput.EndOfStream) {
eventLog.AppendLine(scriptRunProcess.StandardOutput.ReadLine());
}
while (!scriptRunProcess.StandardError.EndOfStream) {
errorLog.AppendLine(scriptRunProcess.StandardError.ReadLine());
}
Log.AddEvent(new List<String>() { "Info" }, eventLog.ToString());
if (errorLog.Length > 0) Log.AddEvent(new List<string>() { "Info", "Warning", "Critical" }, errorLog.ToString());
}
The R scripts I run use RODBC to connect to a database to get data. However whenever I run a query through my RODBC connection it prints the ODBC driver logging info (which has tracing set to 0) to the process StandardError. How can I either just make it log this to info or not log it at all? I want to be able to use Standard error to actually detect errors, but I can't with this happening as there is always an error stream.
RODBC is not doing the logging; this is caused by the ODBC connector (in this case, the Snowflake ODBC connector) trying to open a log file which is already opened by another script's ODBC connector, and instead logging to stderr without filtering.
You can see this by running two scripts at once that use the ODBC connector. Setting TRACE=1 as an environmental variable for the second will also give simba logs, which include a "cannot open file message" - using procmon will show that the file in question is the default log file.
I have a native EXE that converts a file based on command line arguments. Provided, I know how to give full path names of the input and output files, can I run such an EXE from my app service when some button is pressed and wait till the output file is created? Converting to DLL is not an option.
As far as I know, we could run a native exe in the azure app service.
But we couldn't directly pass the parameter to the native exe.
You need write a web application or something else for the user to type in the input parameter.
Then you could use Process.Start method to run the exe.
About how to do it , you could refer to this code sample.
I use ASP.NET MVC to get the input parameter then send the parameter to the exe and get the result.
public ActionResult Index()
{
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = Server.MapPath("/exe/Sum.exe"),
//Arguments could be replaced
Arguments = "1 2",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
string line = proc.StandardOutput.ReadLine();
// do something with line
Response.Write( " The result is : " + line);
}
//await getResultAsync();
return View();
}
Result:
I have an exe file which I run through windows command prompt and give command line arguments. I went through this post and ran the following command:
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
But all it did, is to give me resource files located in WindowsFormsApplication1\obj\Debug folder
I went through this post but it tells on how to execute the exe directly without the running it from cmd.
I even tried the following command:
string path = Path.Combine(Path.GetTempPath(), "MyApplication.exe");
It worked but after clearing my C:\Users\UserName\AppData\Local\Temp folder the application started giving an error.
I even tried the following command:
global::ApplicationName.Properties.Resources.MyApplication
but it gives byte[] and not the path to the application.
All I want to know is how to run the application which is embedded in my resources so that I can successfully execute the following command:
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/K " + MyApplication+" Argument "+Path1+" "+Path2 ,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
string line = proc.StandardOutput.ReadToEnd();
using (System.IO.StreamWriter file = new System.IO.StreamWriter(resultFile))
{
file.WriteLine(line);
}
}
Extract the resource into a file in the filesystem and then run it.
byte[] fileContents = ApplicationName.Properties.Resources.MyApplication;
File.WriteAllBytes("MyApplication.exe", fileContents);
Now you can run the file using MyApplicaton.exe as path.
I used innosetup to install my application.
All the files for example are in program files\test
In the directory i have the exe file of my program and also ffmpeg.exe
Now in my code i did :
class Ffmpeg
{
NamedPipeServerStream p;
String pipename = "mytestpipe";
byte[] b;
System.Diagnostics.Process process;
string ffmpegFileName;
string workingDirectory;
public Ffmpeg()
{
workingDirectory = Path.GetDirectoryName(Application.LocalUserAppDataPath) + #"\workingDirectory";
ffmpegFileName = #"\ffmpeg.exe";
if (!Directory.Exists(workingDirectory))
{
Directory.CreateDirectory(workingDirectory);
}
ffmpegFileName = workingDirectory + ffmpegFileName;
Logger.Write("Ffmpeg Working Directory: " + ffmpegFileName);
}
public void Start(string pathFileName, int BitmapRate)
{
try
{
string outPath = pathFileName;
Logger.Write("Output Video File Directory: " + outPath);
Logger.Write("Frame Rate: " + BitmapRate.ToString());
p = new NamedPipeServerStream(pipename, PipeDirection.Out, 1, PipeTransmissionMode.Byte);
b = new byte[1920 * 1080 * 3]; // some buffer for the r g and b of pixels of an image of size 720p
ProcessStartInfo psi = new ProcessStartInfo();
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.FileName = ffmpegFileName;
psi.WorkingDirectory = workingDirectory;
Thep roblem is that the directory workingDirectory not contain the ffmpeg.exe after installation . so if the user will run first time the program after installation the file will be missing .
I added the ffmpeg.exe to my project and set it to : Content and Copy always
What i want to do is that somehow to set the workingDirectory to the place where the user was installing the program if it's program file or any other directory .
Or to set the workigDirectory to the file ffmpeg.exe i already added to the project.
The problem is after installation the user will run the program and the directory workingDirectory will be empty .
if the file ffmpeg.exe is installed in the same directory where the assembly that calls it resides, then your could write:
string fullPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string installDirectory = Path.GetDirectoryName( fullPath );
However if you really want to copy that file from the installed directory to the Application.LocalUserAppDataPath
// Assuming that the LocalUserAppDataPath has already been created
string destDirectory = Path.Combine(Application.LocalUserAppDataPath, "workingDirectory");
File.Copy(Path.Combine(installDirectory, "ffmpeg.exe"),
Path.Combine(destDirectory, "ffmpeg.exe"), true);
but then, why you don't search the functionality of InnoSetup to discover how to place the ffmpeg.exe file in the workingDirectory during setup? That will solve all your issues here.
1- You should create a registry key that can store the installation path and path of any other folder that your application needs. Check this question on how to do that: How to write install path to registry after install is complete with Inno setup
2- Read the registry settings at application startup. Use those paths.
You can use Application.StartupPath to reference the folder path where are your main executable and ffmpeg.exe
Application.StartupPath
Gets the path for the executable file that started the application,
not including the executable name.
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx
And then if you need it... copy the ffmpeg.exe to the working directory that you choose.. though I think its not a good idea...
File.Copy(Source, Target)
http://msdn.microsoft.com/en-us/library/c6cfw35a.aspx
string destDirectory = Path.Combine(Application.LocalUserAppDataPath, "workingDirectory");
string ffmpegDestPath = Path.Combine(destDirectory, "ffmpeg.exe");
string ffmpegSrcPath = Path.Combine(Application.StartupPath, "ffmpeg.exe");
if (!File.Exist(ffmpegDestPath)) {
if (!Directory.Exists(destDirectory)) Directory.Create(destDirectory);
File.Copy(ffmpegSrcPath , ffmpegDestPath );
}
What I Have
I am currently writing a program which takes a specified file and the performs some action with it. Currently it opens it, and/or attaches it to an email and mails it to specified addresses.
The file can either be of the formats: Excel, Excel Report, Word, or PDF.
What I am currently doing is spawning a process with the path of the file and then starting the process; however I also am trying to fix a bug feature that I added which adds the verb 'PrintTo' to the startup information, depending on a specified setting.
What I Need
The task I am trying to accomplish is that I would like to have the document open and then print itself to a specified printer named within the program itself. Following that up, the file should then close itself automatically.
If there is no way to do this generically, we might be able to come up with a way to do it for each separate file type.
What you Need
Here is the code I'm using:
ProcessStartInfo pStartInfo = new ProcessStartInfo();
pStartInfo.FileName = FilePath;
// Determine wether to just open or print
if (Print)
{
if (PrinterName != null)
{
// TODO: Add default printer.
}
pStartInfo.Verb = "PrintTo";
}
// Open the report file unless only set to be emailed.
if ((!Email && !Print) || Print)
{
Process p = Process.Start(pStartInfo);
}
How I'm doing...
Still stumped... might call it like Microsoft does,'That was by design'.
The following works for me (tested with *.doc and *.docx files)
the windows printto dialog appears by using the "System.Windows.Forms.PrintDialog" and for the "System.Diagnostics.ProcessStartInfo" I just take the selected printer :)
just replace the FILENAME with the FullName (Path+Name) of your Office file. I think this will also work with other files...
// Send it to the selected printer
using (PrintDialog printDialog1 = new PrintDialog())
{
if (printDialog1.ShowDialog() == DialogResult.OK)
{
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(**FILENAME**);
info.Arguments = "\"" + printDialog1.PrinterSettings.PrinterName + "\"";
info.CreateNoWindow = true;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
info.UseShellExecute = true;
info.Verb = "PrintTo";
System.Diagnostics.Process.Start(info);
}
}
Theoretically, according to an article on MSDN you should be able to change it to be along the lines of (untested):
// Determine wether to just open or print
if (Print)
{
if (PrinterName != null)
{
pStartInfo.Arguments = "\"" + PrinterName + "\"";
}
pStartInfo.CreateNoWindow = true;
pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pStartInfo.UseShellExecute = true;
pStartInfo.WorkingDirectory = sDocPath;
pStartInfo.Verb = "PrintTo";
}
get from Rowland Shaw:
ProcessStartInfo startInfo = new ProcessStartInfo(Url)
{
Verb = "PrintTo",
FileName = FilePath,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true,
Arguments = "\"" + PrinterName+ "\"",
};
Process.Start(startInfo);
FilePath look like 'D:\EECSystem\AttachedFilesUS\53976793.pdf'
PrinterName is your printer name
copy the code,it will work.