Click here to Skip to main content
15,924,829 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi folks, I have a vb.net problem that has me stumped. I have an operator interface PC that I have a program running on that calls other programs using the Process.Start(anotherApp.exe) command. For example, the main program THEMCP.exe (master control program for anyone who remembers Tron) calls ProgramA.exe which is located in a sub-folder of where THEMCP.exe resides. Everything resides under C:\Program Files...

ProgramA.exe reads two text files when it starts up, and the files contain parameters so that i can use ProgramA.exe for several uses depending on the content of the files. ProgramA.exe checks to see if the files are present, then reads them. When i run ProgramA.exe manually by taking over the PC and double-clicking on the ProgramA.exe in Windows Explorer/File Manager, the program runs fine, sees the parameter files and all is well. When I schedule ProgramA.exe using Process.Start() it fails to find the files.

Is this a windows permission problem or is there a better way to run ProgramA.exe than using Proces.Start()? ProgramA.exe is using File.Exists(filename) to check to see if the files are present. I don't get why it works when ran manually but it fails when called by Process.Start. Help?

What I have tried:

I've tried different paths in the "If File.Exists()" to no avail. Regardless of what I use - just the file name - the full path with the file name - Application.StartupPath, it always work fine if run manually but it fails if called by Process.Start().
Posted
Updated 23-Oct-17 6:13am
Comments
Richard Deeming 20-Oct-17 14:35pm    
How did you create the files? Since you're running under the "Program Files" directory, if the program that created them wasn't elevated, and doesn't have a manifest, they won't actually exist in the folder thanks to UAC virtualization:

I saved some files into the Program Files directory, and now they’re gone! | Raymond Chen[^]

Check to see if the files are in a sub-directory under %LOCAL­APP­DATA%\Virtual­Store.
kuratazen 20-Oct-17 15:25pm    
Thanks for responding Richard.

The files are simple text files created with Notepad. I copied them into the folder manually. They just contain one line of text with the paths where files will be copied from and to. Nothing overly complicated. I have to collect log files from multiple machines (each with their own PC) and figured this would be an easy way to do it. So as an example I currently have four folders for four machines - each has the same program (ProgramA.exe in this example) but the files have different paths in them - I'm just using the text files so I can re-use the program rather than making four duplicate programs with the paths hard coded. Maybe I need to add the folders to the system's Path statement or something like that?

1 solution

The problem is one of ProgramA is assuming the files are in the "current directory". That directory is not what you think it is.

You're launching your MCP program from a certain Program Files directory. You then launch ProgramA from a subdirectory. The "current directory" is NOT the one ProgramA was launched from. It's the MCP directory, where your files are not.

You have to get into the habit of building and using fully qualified paths when doing file operations. DO NOT just specify a filename and call it good. You just ran into why that's a bad idea.

In ProgramA, get the startup path and use Path.Combine to add your filename to it. You can get the startup path with the first answer here[^] .
 
Share this answer
 
Comments
kuratazen 23-Oct-17 12:09pm    
Thanks Dave - actually that's not the solution. I do see what you're saying, and I'll agree that having the full path is proper. Once I corrected the path, the called program still did not copy files like I wanted but would work when run manually.
Dave Kreskowiak 23-Oct-17 12:14pm    
Well, without exception messages, it's pretty much impossible for anyone to tell you what's going on.
kuratazen 23-Oct-17 15:09pm    
Actually I think you are correct - my apologies. After some fumbling, I've determined that ProgramA actually IS looking in the folder that the MCP is running from instead of looking for the files in the same folder as ProgramA is located. I was using "Environment.CurrentDirectory" and combining that string with my file name, so no I wasn't just hoping for the best. I will take a look at the reference and try again. When it's working I'll post what the working combination is.
kuratazen 23-Oct-17 15:33pm    
Application.StartupPath was the fix in ProgramA. Blarg! I would have bet money that I had tried that already. Thanks Dave Kreskowiak.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900