开发者

Passing a C# string to VBscript

I am using a windows form and am trying to pass a string to a vbscript. The program is asking the user to select a folder, I am trying to take the folder selection and pass it through to the vbscript.

C# Code:

String SelectedFolder = @"C:\Users";

        folderBrowserDialog1.SelectedPath = SelectedFolder;
        if (folderBrowserDialog1.ShowDialog() == D开发者_StackOverflowialogResult.OK)
            //Set selectedFolder equal to the folder that was choosen
            SelectedFolder = folderBrowserDialog1.SelectedPath;
    //Call VBScript
     System.Diagnostics.Process.Start(".vbsPath");

VBScript:

TargetFolder = Request.QueryString("SelectedFolder")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(TargetFolder)
Set colItems = objFolder.Items
For Each objItem in colItems
objItem.InvokeVerbEx("Print")
Next

Any help would be greatly appreciated. Thank you


C# side

Use the Process.Start overload that accepts command-line parameters:

System.Diagnostics.Process.Start("C:\path\to\my.vbs", selectedFolder);

If the selectedFolder can contain spaces (which is likely to happen), you should enclose the argument in quotes.

System.Diagnostics.Process.Start("C:\path\to\my.vbs", 
    "\"" + selectedFolder + "\"");

In fact, if the path can contain quotes and/or trailing backslashes, escaping gets a lot more complicated, see these questions (and others) for details: Escape command line arguments in c#, Passing command-line arguments in C#.


VBScript side

In your VBScript, read the first command line parameter:

targetFolder = WScript.Arguments.Item(0)


You could write the string to a file which both sets of code can access, or a database. That way it won't matter which programming language you are using at all e.g. could be C# to PHP.

Write file:

string content = "folder=" + folder;
System.IO.StreamWriter file = new System.IO.StreamWriter(@"c:\config.txt");
file.WriteLine(content);
file.Close();

Read file:

System.IO.StreamReader file = new System.IO.StreamReader(@"c:\config.txt");
string content = file.ReadToEnd();
// extract value of folder setting here
file.Close();

(Of course the reading would need to be in VB, but would be very similar. Note: code based on: MSDN Example)


Going back and forth from VBScript to C# is adding a lot of complexity. If possible, it would really be easier to choose one or the other.

You can do anything in C# that you can in VBScript. (How you do it is probably different, but you can do all the same tasks - file access, database access, etc.) If it's feasible, you may be better off just working in C#.

I'm doing a bit of a guess based on the context of the question here, but I'm trying to answer in my own head why you'd want to do this, and the only thing I can think of is that you don't know how to display a folder dialog box in VBScript, so you're resorting to trying to do it in C#. Is that correct?

If so, you can show a folder dialog in VBScript as shown here: http://www.robvanderwoude.com/vbstech_ui_browsefolder.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜