-2

I'm creating a File in the C# using Files library, For example I have some vbscript which specifies the filename and calls the C# code, The C# is responsible for creating the file ("C:Users\temp\abc.txt") and what IF i want to specify the filename as (".\abc.txt") in the vbscript and would like the C# to be able to create the file in the VBSCript directory.

Alpha
  • 11
  • 1
  • 1
    Depends on how you launch your VBScript and how you launch your C# code. The initial working directory of a process is specified when the process is created. – Wyck Dec 20 '21 at 17:59
  • Thanks for the reply though, I'm launching the C# code from the Vbscript command line. – Alpha Dec 20 '21 at 18:25
  • 1
    What's the command line you enter? – Wyck Dec 20 '21 at 20:07

1 Answers1

1

Option 1: Keep the VBScript and C# programs in the same directory and set that as the current directory in the vbs before running the C# program. That will allow you to use the same relative paths in both programs.

Const Visible = 1
Const Hidden = 0
Const WaitForCompletion = True
Set oWSH = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
oWSH.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
oWSH.Run "mycsprog.exe", Visible, WaitForCompletion

Option 2: Pass the current directory as an argument to the C# program:

Const Visible = 1
Const Hidden = 0
Const WaitForCompletion = True
Set oWSH = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
CurDir = oFSO.GetParentFolderName(WScript.ScriptFullName)
oWSH.Run "c:\pathtoprog\mycsprog.exe """ & CurDir & """", Visible, WaitForCompletion

C#:

string CurDir = args[0];
LesFerch
  • 1,540
  • 2
  • 5
  • 21
  • 2
    Didn’t [you answer](https://stackoverflow.com/a/70043076/692942) a similar question not that long ago? – user692942 Dec 21 '21 at 09:03
  • @user692942 Yes, but this question isn't exclusively asking for a relative path solution. It isn't clear if the OP wants to keep the vbs and exe in the same folder or in separate locations. Passing an absolute path via the command line may be the preferred solution. – LesFerch Dec 21 '21 at 14:03