C# process.Start filename and passing arguments

2019-02-28 01:17发布

问题:

I need to open the file test.mdb. The path must be fullpath built from whatever directory it is located in relative to the C# program exe

I need to pass the Arguments line which in batch would be passed like this /cmd "MyArgument" For the /cmd switch to work the path must include the full path to the installed MSACCESS.EXE

I just can't work out how to pass the full path to MSACCESS>EXE followed by the fullpath to test.mdb followed by the Arguments.

To try to help I'm posting the following batch string which works but I need C#

"C:\Program Files\Microsoft Office\Office10\MSACCESS.EXE" "C:\Documents and Settings\User\Test Examples Folder\test.mdb" /cmd "MyArgument"

To make clear two important points:

The test.mdb fullpath must be automatically determined by the launching C# exe it will be the C# exes path but with test.mdb.

And the MSACCESS>EXE path must be the full path to the installed version of MSACCESS.EXE if the C# exe program can automatically check which version i.e. Office 10, Office 12 etc and use that this would be excellent.

This is my code so far:

var filePath = @"test.mdb";

Process process = new Process();
process.StartInfo.FileName = filePath;
process.StartInfo.Arguments = "/cmd " + "\"MyArgument\"";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();

回答1:

I don't think you need to worry about detecting the office version, just this should work for you:

        string filepath = '"' + Directory.GetCurrentDirectory() + "\\test.mdb" + '"';
        string acc_cmd_arg = "HELLO";

        using (System.Diagnostics.Process process = new System.Diagnostics.Process() )  {
            process.StartInfo.FileName = "msaccess.exe";
            process.StartInfo.Arguments = filepath + " /cmd " + acc_cmd_arg;
            process.Start(); 
        }


标签: c# ms-access cmd