Launching JAR with Process.Start: javaw.exe runs but "nothing happens"? - c#

Given the code. when the button is clicked nothing happens , i get no debug message etc in visual studio. However if i were to double click the .jar file in its folder i am able to run it. Anyone have any idea why?
Looking at Task manager when the button is clicked. javaw.exe is created but nothing happens.
private void btnKinderPuzzle_Click(object sender, RoutedEventArgs e)
{
// Check if this program is opened
if (IsProcessOpen("MTPuzzle"))
{
MessageBox.Show("KinderPuzzle is already running", "Kinder Package", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
Process.Start(Directory.GetCurrentDirectory() + "\\Puzzle\\PuzzleGame\\MTPuzzle.jar");
}
}

Process.Start("java.exe",
Path.Combine("-jar " + Directory.GetCurrentDirectory(),
"Puzzle\\PuzzleGame\\MTPuzzle.jar"));

The path may be not correct. You might use instead
Process.Start(Path.Combine(Directory.GetCurrentDirectory(), "Puzzle\\PuzzleGame\\MTPuzzle.jar"));
If stills, then I think the problem in the setting of Java. To solve this potential problem, create a file run.cmd near your jar file and write this code into:
java -jar "MTPuzzle.jar"
Then, use Process.Start to start the file run.cmd

Related

C# - OpenFileDialog too slow only first time on pressing OK

I have a button which opens an OpenFileDialog. When I compile the application, run it for the first time, press the button, select file(s) and then press the accept dialog button, it waits for about a minute before adding the selected file(s) into my list box.
If I close the application, restart it and do the same thing as above, everything works fast and normal. From then on it always works fast. It's only the very first time I run it after the compilation when it's too slow.
The code extract is below. What could be wrong with the dialog? Why does it run slowly for the first time? Thank you.
void ButtonAddClick(object sender, EventArgs e)
{
this.openFileDialog.FileName = String.Empty;
this.openFileDialog.InitialDirectory = this.openPath;
if (this.openFileDialog.ShowDialog() == DialogResult.OK)
{
foreach (string file in this.openFileDialog.FileNames)
{
if (!File.Exists(file))
{
this.ShowStatus("Error occured selecting file " + Path.GetFileName(file));
}
else if (!this.listBoxFiles.Items.Contains(file))
{
this.listBoxFiles.Items.Insert(0, file);
}
else{
this.ShowStatus("File " + Path.GetFileName(file) + " already selected");
}
}
}
if (this.listBoxFiles.Items.Count > 0)
{
this.openPath = Path.GetDirectoryName(this.listBoxFiles.Items[0].ToString());
this.listBoxFiles.Enabled = true;
this.buttonClear.Enabled = true;
this.buttonFolder.Enabled = true;
}
}
If you are facing this problem of slowness at first initialisation, my suggestion is listed below to rectify this.
For VS debugging, in Visual Studio IDE, just go to Tools>Options>Debugging. Find settings page named [Symbols]. Click it to load the page content. On the right side panel if “Microsoft Symbol Servers” checkbox is checked, just uncheck it and press “Ok” to save the settings. Now on running compiled exe, if the problem still persists then I suggest to perform some cleanup operation on your pc. Also make sure that all timely VS updates are incorporated in your VS. Let me know if this helps if not we can figure out something else.
Try calling Directory.GetFiles(folderPath); prior to showing the OpenFileDialog window. That might trigger the same caching that's occurring after you do OpenFileDialog the first time with your current method.

C# Winform application couldn't run when Window startup

everyone
I have a problem with my application. I make an event that when I tick to a checkbox it will run when Window startup and I save this setting in a XML file. But it doesn't work and Window show me a message error "Stop working". Does anyone know what did I do wrong? I try to resolve it but it still. Thanks a lot. Here is my code:
private RegistryKey registrykeyApp = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Windows\CurrentVersion\Run", true);
private void checkBoxKhoidongcungwin_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBoxKhoidongcungwin.Checked)
{
if(this.registrykeyApp.GetValue("ViKey") == null)
this.registrykeyApp.SetValue("ViKey", Application.ExecutablePath.ToString(),RegistryValueKind.ExpandString);
}
else
{
this.registrykeyApp.DeleteValue("ViKey", false);
}
}
That registry key cannot be accessed from an application that is not running with elevated privileges since viruses, etc could use it to hijack computers. Unfortunately when access is denied you often get generic errors that aren't obvious.
Right click on your exe and run it as administrator and it should work. If so you can add an application manifest to your project to always request the elevated privileges automatically, without manually right clicking.
http://www.samlogic.net/articles/manifest.htm

getting the full filenames from all top directory files in a specific directory and suppressing the occuring error

I met a not expected problem with getting just the top directory full filenames from a specific directory. C# throws an error and doesn't list anything in the specific directory.
But MS DOS has not a problem with my command: *"dir C:\windows\prefetch\*.pf"
Visual Basics 6 old "Dir Function" also does it without complaining.
The "Windows Explorer" opens it up and doesn't ask anything from me. Also "Nirsofts Tool Suit" lists it instantly without any problem. No one of this tools needs to run with special permissions, just a double click on the application icon and ready is the task.
I looked around and found nothing here, what would answer this weird problem. My user can access the directory, if I go with any other application into it, now there is the question why C# throws
an "Unauthorized Access Exception" which is totally weird, since I have access in this folder.
I don't want to elevate my application with admin permissions for it nor create extra a xml for it to run it with highest privileges. The not trustful yellow elevation box must be avoided.
Now my question: How it comes that I can not list the filenames in this folder when all other
applications can do that.
What code do I need if "Directory.GetFiles()" fails?
Is there any flag or property in the framework directory class which allows my application access to the files, whatever.
Here my code which fails (using System.IO):
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text.Substring(0, 0); //clear the textBox1
//Unauthorized access exception and yellow bar in this line
foreach(string FileX in Directory.GetFiles(Path.Combine(Environment.GetEnvironmentVariable("windir"), "prefetch"), "*.pf"))
{
textBox1.Text += FileX;
}
}
Did I understand correctly that you only need the File-names with directory-names.
This code works for me, no elevations needed.
private void button1_Click(object sender, EventArgs e)
{
string folder = #"C:\windows\prefetch";
string filemask = #"*.pf";
string[] filelist = Directory.GetFiles(folder, (filemask));
//now use filelist[i] for any operations.
}

Button Click no result

I was following a tutorial on XDA [http://forum.xda-developers.com/showthread.php?t=2042227] in regards to setting up your own android ADB GUI Toolkit in C#. I have the GUI setup right as we speak and the code compiles correctly but when I click the install APK button it does not install the APK to my device.
private void InstallAPK_Click(object sender, EventArgs e)
{
var process = Process.Start("CMD.exe", "/c adb install " + textBox1.Text);
process.WaitForExit();
MessageBox.Show(".APK is Installed", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
No errors are found in this code it all works but its not actually executing the ADB command as it is told to. - Any advice would be helpful!
You need to add quotation marks around the file name - otherwise it would not properly process file names containing spaces.

Problem with running exe file from c#

When I execute an exe file (PVFProject15.exe), it reads the data from an input file (inputFile.txt) and print the results in another file (outputFile.txt). The exe file works well when I double click it; It opens the console window which stays opened until the output file is created. However, when I run (PVFProject15.exe) from c#, the console window opens and closes very quickly and the output file is never created.
I would really appreciate your help since I have been working to fix this for a whole day and never found the answer. Here is my code below.
private void button1_Click(object sender, EventArgs e)
{
Process runFortran = new Process();
try
{
runFortran.StartInfo.FileName = "C:\\temp\\trial\\PVFProject15.exe";
runFortran.Start();
runFortran.WaitForExit();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
Thank you in advance.
Safaa
Probably PVFProject15.exe needs current directory to be set to C:\temp\trial
If PVFProject15.exe writes to file using relative path, look for outputFile.txt in directory from which you start your main program-bootstrapper.
I also meet with same problem, when I try start some .exe and .hta from my C# based software.
I start to looking for solution and answer of Mike Mozhaev get to me right direction.
In your code you need to use:
StartInfo.WorkingDirectory = Convert.ToString( System.IO.Directory.GetParent(appPath));
So code have to be like this:
if (File.Exists(appPath))
{
Process runProcess = new Process();
runProcess.StartInfo.WorkingDirectory = Convert.ToString( System.IO.Directory.GetParent(appPath));
runProcess.StartInfo.UseShellExecute= true;
runProcess.StartInfo.FileName = appPath;
runProcess.Start();
}

Categories