c# - Why does using Process.Start makes the started app run from the parent app location instead of the started app's actual location? -
I have run into a problem where for another application Why does this happen? Is it deliberate design? It is not necessary to say that the targeted app is completely screwed, so how should this method of launching any other XE should be used? Here's an example to demonstrate the problem: Let's create a simple small app that searches for a file and "Success" , And if it does not get it, then "fails". The desired folder structure of the above program looks like this: The program goes to the Test folder, if it exists, and searches for the test.txt file. We are now a quick launcher app that will be used to run our testing app: The purpose of the above application is to stay in the same directory as our Testimonials.exe. When run in such a way, everything works fine properly. Runs Though , we now make some changes to our launching application. Our launching application will now remain at the top of our test.exe directory level, such as: Naturally, we also change our longpap to target our Testapp.exe in a separate directory, such as: Now when At this point, if we go back to our Indicates that Why does this happen, and how can I prevent it from happening? I want to be able to run an application located in a separate folder where my calling application remains, and thus it is clearly important that the initial process runs from where the process is located, and where the calling application is not Is located. The process acquires the work folders of those processes that start with them by default. This is just the way the windows work (yes, it is purposely done in such a way that other operating systems behave in the same way.) You can use process.start Using that, the app must warn about the missing files and then the crash. After a little investigation and debugging it seems that, by any means, the
process When using Start , it is run from the location of the targeted unit that is run from the original program location, Exe is actually located.
using the system; Using System.IO; Using System.Windows.Forms; Namespace testap {public partial class} form 1: form {public form 1 () {initial group (); } Private Zero Button 1_Click (Object Sender, EventArgs E) {try (if.exists (@ ".. test \ test.txt")) {MessageBox.Show ("program executes successfully"); } Else {MessageBox.Show ("The program failed to execute properly"); }} Hold (Exception Pre) {Message Box. Show (ex.ToString ()); }}}}
Testapp folder -> Testapp.exe - & gt; Test folder - & gt; Test.txt
using the system; Using System.Diagnostics; Using System.Windows.Forms; Namespace Launch Test App {Public Partial Class} Form 1: Form {Public Form 1 () {Initialize Companent (); } Private Zero Button 1_Click (Object Sender, EventArgs E) {try.start ({@ ". Testapp.exe"); } Hold (Exception pre) {Message Box. Show (ex.ToString ()); }}}}
Launchpap our
testup , and we can click on the button in our
Testapp and view the program box successfully executed Are there.
Launching an app folder - & gt; Launpepe Exchange - & gt; Test folder - & gt; TestAP.XA - & gt; Test folder - & gt; Test.txt
Process.Start (@ "\ TestApp \ Testapp.exe.");
launches is run, and as a result, it runs
Testapp.exe , and we press our button, our < code>
Testapp , then code and
. Test \ test.txt \ to
. Change the location to search for test.txt from Testapp \ test \ test.txt , then run the program successfully.
Testapp exe is running from the place of
Launchapp , instead of the place where
Testapp exe Actually located.
. Working directory > Proststart Infos to start it from any other route.
Comments
Post a Comment