程序以process.start开头,看不到本地文件 [英] Program started with process.start does not see local files

查看:92
本文介绍了程序以process.start开头,看不到本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个让我难过的vb.net问题。我有一个操作员界面PC,我有一个程序在运行,使用Process.Start(anotherApp.exe)命令调用其他程序。例如,主程序THEMCP.exe(任何记住Tron的人的主控程序)调用ProgramA.exe,它位于THEMCP.exe所在的子文件夹中。一切都在C:\程序文件下...



ProgramA.exe在启动时读取两个文本文件,文件包含参数,以便我可以使用ProgramA.exe有多种用途,具体取决于文件的内容。 ProgramA.exe检查文件是否存在,然后读取它们。当我通过接管PC并双击Windows资源管理器/文件管理器中的ProgramA.exe手动运行ProgramA.exe时,程序运行正常,看到参数文件一切正常。当我使用Process.Start()安排ProgramA.exe时,它无法找到文件。



这是一个Windows权限问题还是有更好的方法来运行ProgramA .exe比使用Proces.Start()? ProgramA.exe使用File.Exists(filename)来检查文件是否存在。我不明白为什么它在手动运行时有效但在Process.Start调用时失败。帮助?



我的尝试:



我试过了如果File.Exists()中的不同路径无济于事。无论我使用什么 - 只是文件名 - 文件名的完整路径 - Application.StartupPath,如果手动运行它总是工作正常但是如果由Process.Start()调用它会失败。

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().

推荐答案

问题是ProgramA假定文件位于当前目录中。该目录不是您认为的目录。



您正在从某个Program Files目录启动MCP程序。然后从子目录启动ProgramA。 当前目录不是ProgramA的推出者。这是MCP目录,你的文件不是。



你必须养成在进行文件操作时构建和使用完全限定路径的习惯。不要只指定文件名并称之为好。你只是碰到了为什么这是一个坏主意。



在ProgramA中,获取启动路径并使用Path.Combine为其添加文件名。您可以通过第一个答案获得启动路径此处 [ ^ ]。
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[^] .


这篇关于程序以process.start开头,看不到本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆