2

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

2
  • 1
    Request.QueryString? so you want to pass the string to a classic-asp page written in vbscript as a url argument? Commented Sep 28, 2011 at 13:33
  • I'm sorry, I am very new to vbscript and have been looking for a way to access the string; the Request.QueryString was the only thing that I have found that looked like it might work but does not. Is there any way to pass the C# string to VBscript? Commented Sep 28, 2011 at 13:59

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Trying it out now. Thank you very much for your help!
@dhk: You're welcome! Don't forget to click on the check mark next to my answer, so that others know that this question has been answered.
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)

Comments

0

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

1 Comment

Thank you for the reply, I am trying to cycle through each file in a folder and print them, I know it can be done in VB.net but I cannot find the equivalent in C#.net. Looking back at it now, I should have phrased this question differently(Sorry I'm quite new to the site).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.