C# Loading an exe on a file share - c#

I am trying to run a program on a Citrix VM located at \\site.local\shares\Agent_console\AC_Launch\AC_Launcher.exe /env=PROD
I am currently using the code below and it is stating the following
private void button13Chromatix_Launcher_Click(object sender, EventArgs e)
{
Process.Start(#"\\plumbedford.local\shares\Agent_Console\AC_Launch\AC_Launcher.exe",
"/Env=PROD");
}
But that fails with the following error:
Unable to access Agent Console XML network share. Try again?
This is a desktop shortcut that works using the same directory.
Thank you in advance.

Try using a literal string (with an # prefix), to ensure that backslashes are not misinterpreted.
It also looks like you have some typos in the location.
Try this:
Process.Start(#"\\site.local\shares\Agent_Console\AC_Launch\AC_Launcher.exe", "/env=PROD");

Related

C# How to put another exe in Windows startup from code

I never needed to do this before and the examples i saw only show me how to put my current app from code in startup but not other .exe file in startup using code from current app
you can startup anything you want via using c# code using Process class like...
private void btnOpenApp_Click(object sender, EventArgs e)
{
System.Diagnostics.Procces.Start("appName.exe");
}

Environment Variables in web.config

Working on a webapp on VS2017 using MVC framework on .NET 4.5. In my local dev environment, I use the web.config file something like this:
<appSettings>
<add key="GOOGLE_APPLICATION_CREDENTIALS"
value="C:\\Work\\Services-abcd.json" />
</appSettings>
But when I run the webapp I still get the error that GOOGLE_APPLICATION_CREDENTIALS is undefined.
So my question specifically is:
Where am I going wrong? Do I need to define it somewhere else?
When I deploy this to staging/production, on my azure web service, how will I share the json file and how will the web.config file need to change for that?
I know this question answers how to get the variables in web.config, but somehow I am not able to set it there.
Edit 1: Since I was asked, here is where I am getting the error of missing environment variable-
using Google.Cloud.Translation.V2;
TranslationClient client = TranslationClient.Create();
I am unable to declare TranslationClient.
Where am I going wrong? Do I need to define it somewhere else?
According to your codes and description, I found you have defined the app setting not the environment variables. Notice: app setting isn't as same as environment variables.
If you want to set environment variables in the azure web app, I suggest you could set it in your web application codes and upload the application to the app service as below:
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", Server.MapPath("test/test.txt"));
When I deploy this to staging/production, on my azure web service, how will I share the json file and how will the web.config file need to change for that?
I suggest you could create a folder to store the json file in your web application, then you could use Server.MapPath to get the right path of your json file.
Since I don't have the google cloud app credentials json file, so I add a txt file in my test demo to test the code could work well.
More details about my test demo, you could refer to below codes.
protected void Page_Load(object sender, EventArgs e)
{
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", Server.MapPath("test/test.txt"));
}
protected void Button1_Click(object sender, EventArgs e)
{
string path = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
Response.Write(path + "\n");
string[] lines = System.IO.File.ReadAllLines(path);
foreach (string line in lines)
{
Response.Write(line);
}
}
Result(111 is the txt file content):

Why does using Process.Start makes the started app run from the parent app location instead of the started app's actual location?

I've run into a problem where using Process.Start on an exe file for another application would cause that application to warn about missing files and then crash. After a bit of testing and debugging it looks like, somehow, when using Process.Start, it makes the targeted exe run from the location of the parent program that launched it, rather than from the location where that exe is actually located.
Why does this happen? Is this intentional design? Needless to say this completely messes up the targeted app that you run, so how should this method of launching another exe be used?
Here's an example to demonstrate the problem:
Let's create a simple small app that searches for a file and prints "success" if it finds it, and "fail" if it doesn't.
using System;
using System.IO;
using System.Windows.Forms;
namespace Testapp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (File.Exists(#".\test\test.txt"))
{
MessageBox.Show("Program executed successfully");
}
else
{
MessageBox.Show("Program failed to execute properly");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
The intended folder structure of the above program looks like this:
Testapp Folder
->Testapp.exe
->Test Folder
->test.txt
The program goes into the Test folder, if it exists, and searches for the test.txt file.
Now let's make a quick launcher app that will be used to run our test app:
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace LaunchTestApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Process.Start(#".\Testapp.exe");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
The above application is intended to reside in the same directory as our Testapp.exe. When this is run that way, everything works out perfectly fine. The Launchapp runs our Testapp, and we can click on the button in our Testapp and see a messagebox that the program had executed successfully.
However, now let's make a slight change to our launching application. Our launching application will now reside on one directory level above our Testapp.exe, like so:
Launching App Folder
->Launchapp.exe
->Testapp Folder
->Testapp.exe
->Test Folder
->Test.txt
Naturally, we also change our Launchapp to target our Testapp.exe in a different directory, like so:
Process.Start(#".\Testapp\Testapp.exe");
Now when the Launchapp is run, and it consequently runs Testapp.exe, and we press our button, our Testapp.exe displays a message that the program failed to run properly.
At this point, if we go back to our Testapp code and change the location to search for the test.txt from .\test\test.txt\ to .\Testapp\test\test.txt, then the program will run successfully.
This demonstrates that the Testapp exe seems to be running from the location of Launchapp, rather than from the location of where Testapp exe is actually located.
Why does this happen, and how can I prevent it from happening? I want to be able to run an application located in a different folder from the one where my calling application resides, and thus it is obviously important that the started process runs from the location where that process is located, and not where the calling application is located.
Processes inherit the working folder of the process that started them by default. It's just the way windows works (yes, it is intentionally done this way, other operating systems behave the same way).
You can use the .WorkingDirectory property of a ProcessStartInfo to start it from another path. http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v=vs.110).aspx
In the absence of any other explicit instructions, it will always use the parent process's environment. This is just how it works. If you want to specify the working location on the filesystem for the new process, use the overload that accepts a ProcessStartInfo instance. This allows you to set the working folder along with many other values.
You're misusing the term "run from".
When you run any program, its current directory is the current directory of its parent process, unless specified otherwise.
Usually (from explorer or a command prompt), this is the directory containing the EXE file.
If you start it from your own program, it will be the current directory of your program.
You can explicitly pass an arbitrary current directory in StartOptions.
Use like this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = #"THE DIRECTORY";
Process.start(startInfo);

Not able to run the .exe file created from c#

I have created a Windows form application using C# in visual studio 2010 connecting the database in SQL server . After all my development is done i copy the exe file generated in my machine and pasted in another machine and try to execute it but it is not working .
i can see the process started in task manager but it was closed after 5 seconds . I even tried creating the setup for this application and installed in that machine still i am facing the same issue .
But in my machine it is working perfectly in the both the ways . can any one help me in finding where i went wrong .
Thanks in advance
As you don't provide the error, the answers and comments you are getting are educated guesses.
You should check the event viewer for errors...
This will let you learn what is going on. If you can't fix it, add this info to your question.
As you are not posting exception message, probably you re not properly catching exceptions. Just to be sure surround your main function in a Try/Catch.
In Catch, write some code to dump message exception into a file, or even better use Log4Net. For simplicity just add some code to write to a file now. Something like:
static void Main()
{
try
{
//Your code
}
catch (Exception ex)
{
//Write ex.Message to a file
using (StreamWriter outfile = new StreamWriter(#".\error.txt"))
{
outfile.Write(ex.Message.ToString());
}
}
}
PS: If it is a console application you can survive with Console.Write
Perhaps you have some referenced assemblies that you did not copy along with the application itself.
OR, the connection string is not valid when run from that other machine (if you worked with a local SQL db, or on a network or whatever and it's not accesible on that other machine)
OR, you don't have rights to run it on that other machine.

Not able to connect to webservice from a WP7 emulator

private void button2_Click(object sender, RoutedEventArgs e)
{
WebClient wb = new WebClient();
wb.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wb_DownloadStringCompleted);
wb.DownloadStringAsync(new Uri("http://weather.yahooapis.com/forecastrss?w=2502265"));
}
void wb_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
XElement xmlResult = XElement.Parse(e.Result);
}
This is code i have used. I am getting an error "Unable to connect to the remote server".
I am able to connect to internet from the IE browser in the emulator.
Suggest a suitable solution.
Did you try to restart your emulator ? Because it happens to me : I got this error with the emulator but none with a simple console programm. After a restart of VS, it works.
I cannot reproduce your problem with the code you have provided. Are you sure you're not stuck behind a firewall or something that blocks your request? Try using Fiddler (or a product like it) to see whats going on.
To allow the Emulator in friewall.
Open Control Panel\System and Security\Windows Firewall\Allowed Programs
Click on "Change Settings" to enable "Allow another program"
Browse - > "C:\Program Files (x86)\Microsoft XDE\1.0\XDE.exe"
Tick Public / Private Network or both as you are connected to internet.

Categories

Resources