I am trying to start excel with a file. It works fine when I run it with the same user. But with different user, only excel starts and that also with unknown error.
private void button1_Click(object sender, EventArgs e)
{
SecureString securePwd = new SecureString();
string password = "P#ssw0rd1";
SecureString sec_pass = new SecureString();
Array.ForEach(password.ToArray(), sec_pass.AppendChar);
sec_pass.MakeReadOnly();
ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = "c:\\Program Files\\Microsoft Office\\Office15\\EXCEL.EXE";
ps.Arguments = "c:\\test_folder\\test.xlsx";
ps.WorkingDirectory = "c:\\test_folder";
ps.Domain = "test.local";
ps.UserName = "testuser";
ps.Password = sec_pass;
ps.UseShellExecute = false;
Process.Start(ps);
}
The very same code works perfectly fine by changing the process from
ps.FileName = "c:\Program Files\Microsoft Office\Office15\EXCEL.EXE";
to
ps.FileName = "notepad.EXE";
had there be any rights issue even notepad.exe also should not work.
Knowing that it's an old post, still leaving a reply for someone like me with a similar problem.
Looking at your code, it seems that you may need to set the "ps.UseShellExecute" as "true". I tried a similar code (given below) with a button in my WPF app, and it opens the excel file without issues.
private void Button_Click(object sender, RoutedEventArgs e)
{
string myPath = #"C:\Users\Lenovo\Documents\MyFile.xlsx";
ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = "excel"; // "EXCEL.EXE" also works
ps.Arguments = myPath;
ps.UseShellExecute = true;
Process.Start(ps);
}
And of course, don't forget to add the following line at the top of your .cs script.
using System.Diagnostics;
Well then, Happy coding :)
There doesn't seem to be any problem withe the code. As without changing a bit, it just started working fine again. Simply nothing. This just opens up a question like what the problem was.?
Any suggestions.?
This is very normal thing. if you working for example in company and you open share Excel file with your friend one of you will get information "File is open by another user" you can resolve this situation copy this file to for example C:/Temp and later replace it on share space.
Related
public void getDeviceinfo()
{
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "C:\\Users\\username\\Desktop\\repo\\project\\executable\\ideviceinfo.exe",
Arguments = "-s",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
string line = proc.StandardOutput.ReadLine();
Console.WriteLine(line);
}
}
I want to run a .exe file and get some output from it using C# project.
When I'm having the .exe file within the project, it's executing but not giving any output. But If I keep the .exe outside of the project and if i give that location , then the executable file is executing and output is returned.
I tried keeping the executable in the debug folder also. But same issue.
I want to keep the executable file within the C# project and i want to execute it.
Any help please? Thanks.
First, add the "WorkingDirectory" parameter to the ProcessStartInfo.
If that doesn't solve it then try moving it to a subfolder in your project. Maybe it is referencing a DLL (or some other resource) in your project rather than the system?
The .exe file saved in the project cannot be exported. if your file path is correct. You can try to use "Process.Start(#" ")" object or "ProcessStartInfo" object to open
Code show as below:
private void button1_Click(object sender, EventArgs e)
{
Process.Start(#"C:\Users\Admin\source\structure\bin\idea64.exe");
}
private void button2_Click(object sender, EventArgs e)
{
//Public domain; no attribution required.
ProcessStartInfo info = new ProcessStartInfo(#"C:\Users\Admin\source\structure\bin\idea64.exe");
info.UseShellExecute = true;
info.Verb = "runas";
Process.Start(info);
}
Run the project:
Hope it helps you.
I'm currently working on a Winforms program from Visual Studio that acts as a control panel for a whole bunch of TeraTerm macros. I did a dumb and didn't add my first working version to version control, and now it's stopped working and I have no idea why/how to get back to what I had.
The function is question is
using System;
using System.IO.Ports;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Diagnostics;
...
namespace Test
{
public partial class MainWindow : Form
{
...
private void ImagesButton_Click(object sender, EventArgs e)
{
RunMacro("F:\\Users\\Isaac\\Documents\\LED Sign Commands\\Macros\\StartDisplayImages.ttl");
}
....
private void RunMacro(string userArgument)
{
panel1.Enabled = false;
StatusLabel.Visible = true;
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo()
{
WindowStyle = ProcessWindowStyle.Hidden,
#if DEBUG
FileName = "F:\\Users\\Isaac\\Documents\\LED Sign Commands\\teraterm\\ttpmacro.exe",
#else
FileName = "..\\teraterm\\ttpmacro.exe",
#endif
Arguments = userArgument
};
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
panel1.Enabled = true;
StatusLabel.Visible = false;
}
...
}
}
When run, I see that ttpmacro.exe does start, and if I omit the Arguments assignment then it will prompt me to select a macro; if I select StartDisplayImages.ttl, it will run as expected. If I include it as an argument as above, however, then ttpmacro still opens but immediately closes. No error comes up (and RedirectStandardOutput/Error produce nothing), it's as if ttpmacro accepts the file but won't do anything with it. I've confirmed both filepaths are valid and correct.
While I didn't add version control, I did extract the main file using ILSpy, and my original functions in the working version were:
private void ImagesButton_Click(object sender, EventArgs e)
{
RunMacro("/V ..\\Macros\\StartDisplayImages.ttl");
}
private void RunMacro(string userArgument)
{
panel1.Enabled = false;
StatusLabel.Visible = true;
Process process = new Process();
ProcessStartInfo startInfo = (process.StartInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "..\\teraterm\\ttpmacro.exe",
Arguments = userArgument
});
process.Start();
process.WaitForExit();
panel1.Enabled = true;
StatusLabel.Visible = false;
}
Being from the published release, the filepaths are relative to the folder of the application. Other than that, the only difference seems to be minor syntax in how process.StartInfo is assigned, but I tried reverting that with no luck. Target framework is .NET Core 3.1. The /V flag isn't the issue; removing it simply makes the ttpmacro window visible for the fraction of a second it runs before closing. If I use a commandline execution of the same file (eg start "F:/Users/.../ttpmacro.exe" "F:/.../StartDisplayImages.ttl"), it also runs as expected.
It turned out the problem was the spaces in the full macro filepath, and surrounding it with escaped double quotes resolved the issue. TeraTerm just wasn't telling me that it wasn't finding the file. Should have been obvious, but I was sure it had been working previously when I was debugging, without requiring the quotes.
Im making an application which needs to monitor the filesystem using FileSystemWatcher, to detect how an installation affects the filesystem.
To get rid of noise i want to filter the events that are created by their creating user, and that code is working with the //BUILTIN //Administrator user, which is used by default when doing an installation. But still there are quite a bit of noise. Then i got the idea of creating a specific user that i can use for running the installation file, and filter on that specific user, and thereby getting rid of allmost all the noise.
this is my code for the process creation and start
private void executeInnoInstaller(string path, string fileName)
{
// Use ProcessStartInfo class
ProcessStartInfo installerProces = new ProcessStartInfo();
installerProces.CreateNoWindow = true;
installerProces.UseShellExecute = false;
installerProces.FileName = path + "\"" + fileName + "\"";
installerProces.WindowStyle = ProcessWindowStyle.Normal;
installerProces.UserName = "test";
System.Security.SecureString encPassword = new System.Security.SecureString();
foreach (System.Char c in "test")
{
encPassword.AppendChar(c);
}
encPassword.MakeReadOnly();
installerProces.Password = encPassword;
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(installerProces))
{
exeProcess.WaitForExit();
//int exitCode = exeProcess.ExitCode;
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
this code exits with a access denied.
OS=Windows
Ive already tried to run the installer.exe from the OS filehandler with SHIFT - Rightclick using the specified user, and it works.
VisualStudio is run as administrator.
Ive tried to run the build project exe file as administrator, but it does not work.
Without the user credentials, the code works and uses the //BUILTIN //Administrator account
Does anybody have any idea ?
Thank you beforehand for your time and effort.
This code works if i turn down the UAC securitylevel to the lowest.
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'm having an issue with the following code:
private void Form1_Load(object sender, EventArgs e)
{
cmdOutput = new StringBuilder("");
cmdProcess = new Process();
cmdProcess.StartInfo.WorkingDirectory = #"C:\android-sdk\tools";
cmdProcess.StartInfo.FileName = #"java";
cmdProcess.StartInfo.Arguments = #"-Xmx512m -Djava.ext.dirs=lib\;lib\x86_64 -Dcom.android.monkeyrunner.bindir=..\framework -jar lib\monkeyrunner.jar";
cmdProcess.StartInfo.UseShellExecute = false;
cmdProcess.StartInfo.CreateNoWindow = true;
cmdProcess.StartInfo.RedirectStandardOutput = true;
cmdProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
cmdProcess.StartInfo.RedirectStandardInput = true;
cmdProcess.Start();
cmdStreamWriter = cmdProcess.StandardInput;
cmdProcess.BeginOutputReadLine();
// Even if i fire this later it doesn't work.
cmdStreamWriter.WriteLine(#"print 'Hello World'");
}
The issue is that:
cmdStreamWriter.WriteLine(#"print 'Hello World'");
Is not doing anything. Nothing is being written to the java process.
The output appears to be working fine (tested by loading a script directly to monkeyrunner.jar. But after trying many times I'm not getting any input.
This does work fine if I change the process to "cmd"
I have managed to solve the issue:
From another issue I had I worked out that Jline (A java based command line extension) was being used. After some Googling I found that starting the Java app with:
cmdProcess.StartInfo.Arguments = #"-Xmx512m -Djava.ext.dirs=lib\;lib\x86_64 -Dcom.android.monkeyrunner.bindir=..\framework
-Djline.terminal=jline.UnsupportedTerminal -jar lib\monkeyrunner.jar";
The Amendment Being:
-Djline.terminal=jline.UnsupportedTerminal
This stopped Jline being loaded and allowed the standard input to work correctly again.
More information on -Djline.terminal argument can be found here:
http://jline.sourceforge.net/ - Installation.