Is it possible to run website url using Shell() command? I saw someone post
Shell() can only read the executable path
But regarding to this site http://www.vb6.us/forums/general-questions/attaching-website-links-your-command-button Shell() can used to run the website url.
I have some website url inside my XML file and I tried to run them using Shell() command as my XML file also containing .exe file path. So I am running those .exe file and website url like this
Dim i As Integer, j As Integer
For i = 0 To 9
For j = 0 To 9
If MenuListBox.SelectedItem = MenuListBox(i, j, 0) Then
Shell(MenuListBox(i, j, 1))
End If
Next
Next
I am using array to store each of elements inside my XML file.
So the problem here is, I can only run my .exe files and when running website url it said that
File not found
Even though my path is correct. I did used the Process.Start() also but it only working for the website url, not the .exe file. It returns me this error.
The system cannot find the file specified
Kindly to help me. Thanks in advance.
Process.Start() can be used for url and executables and other files. If you pass a path to a file, like a doc file, it is open with default application. In your case if you pass a url like "http://www.google.com" it will be opened with your default browser.
According to MSDN:
Starting a process by specifying its file name is similar to typing
the information in the Run dialog box of the Windows Start menu.
Therefore, the file name does not need to represent an executable
file. It can be of any file type for which the extension has been
associated with an application installed on the system. For example
the file name can have a .txt extension if you have associated text
files with an editor, such as Notepad, or it can have a .doc if you
have associated.doc files with a word processing tool, such as
Microsoft Word. Similarly, in the same way that the Run dialog box can
accept an executable file name with or without the .exe extension, the
.exe extension is optional in the fileName parameter. For example, you
can set the fileName parameter to either "Notepad.exe" or "Notepad".
opening a url here and here
Quick solution, to get time to check the real solution:
If path.toupper like "*.EXE"
shell path
Else
process.start (path)
End if
But if you have a Win32Exception (show us the full message) ... they used to appear on 32/64 bits issue. etc. But, as Shell is working, I think you have a Credentials issue= permissions of that folder/exe.
Place that exe on another granted location to test.
Thanks to #Capitán CavernÃcola for your suggestion.
If path.toupper like "*.EXE"
shell path
Else
process.start (path)
End if
I took your code and change the path.toupper like "*.EXE" to Path.Contains(".exe") Then
Here is my coding that working all fine now.
Dim Path As String = MenuListBox(i, j, 1)
If Path.Contains(".exe") Then
Shell(Path)
Else
Process.Start(Path)
End If
Thank you all :)
Related
I'm trying to automate a document handling process, and I need to check if there are any files inside a certain folder. The process itself removes the files from the folder once it finishes, so I need it to loop back and check if there are any files left.
So far I've been using a sample file like this:
File.Exists("C:\Users\gcaor\Desktop\OC\150.pdf")
150.pdf is the sample file it's searching for, but is there a way to search for any file at all? So that it returns true if there is a file in the folder and false if there isn't
You can use Directory.EnumerateFiles + Any:
Dim anyFileExist = Directory.EnumerateFiles(path).Any()
This is using standard .NET methods and also stops at the first file found.
I found a similar question but it was 5 years 8 months old, had 2 replies and neither worked for me (VB.Net Read txt file from current directory)
My issue is that when I use the following code:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Username_And_Password_Raw.txt")
Dim usernameAndPassword = Split(fileReader, ",")
I get an error saying:
System.IO.FileNotFoundException: 'Could not find file 'C:\Users\wubsy\source\repos\NEA Stock Page System\NEA Stock Page System\bin\Debug\net6.0-windows\Username_And_Password_Raw.txt'.'
I have tried using all the different Applications.BLANKPath options I can find (ie; StartupPath, CommonAppDataPath, etc.) and they all return essentially the same error only with a different location.
This is the folder layout of my TXT File - I know it's a terrible, incredibly insecure and generally awful way of storing login information but this is just for a NEA so will never ever actually be used
This is the actual path of the TXT File if it helps
C:\Users\wubsy\source\repos\NEA Stock Page System\NEA Stock Page System\Username_And_Password_Raw.txt
The startup path is where your exe is located. That and all supporting files get copied to a binary directory when you compile in visual studio, in your case
C:\Users\wubsy\source\repos\NEA Stock Page System\NEA Stock Page System\bin\Debug\net6.0-windows
But what you're trying to do, reference the file where it sits in your solution, is probably not the best way to do it, and your code above will work (with a change, will mention later) if you change the properties of the file in the solution.
Right click on the file in the Solution Explorer Username_And_Password_Raw.txt, select Properties. Modify Copy to Output Directory to either Copy always / Copy if newer, depending on your requirement. Now that file will copy to the same directory your exe is in, and the code above should work.
Note, when creating a path, don't use string concatenation because you may have too many or too few \; use Path.Combine:
Dim filePath = Path.Combine(Application.StartupPath, "Username_And_Password_Raw.txt"
Dim fileContents = My.Computer.FileSystem.ReadAllText(filePath)
I encountered the following issue:
When accidentally passing a folder path to the Documents.Open function in VBA Word I get the runtime error 5174 as one would expect.
However all files with names that begin with an underscore get deleted in that moment from that folder.
To replicate:
Assume folder C:/Test/
In said folder have two files:
test.txt
_test.txt
In Word VBA execute the command:
Documents.Open("C:/Test/")
(As part of a subroutine or in the immediate window.)
Result: Runtime Error 5174 and _test.txt is now missing.
Note: Passing a nonexisting file like "C:/Test/abc.txt" or a wrong folder path like "C:/Test" (without the last slash) will not have this effect and won't cause the runtime error.
I have only tested this on one system on a network drive under windows 10 with Microsoft Professional Plus 2019. It's entirely possible that it's an issue with the file system. Does anyone have any suggestions as to why is this happening? I now included the workaround to check if I'm passing a folder, but it's still unnerving.
The Documents.Open method opens the specified document and adds it to the Documents collection. It is designed to take the file path, not a folder. If you want to allow users to pick file(s) you may consider using the file open dialog. The FileOpenDialog triggered by your code which opens a folder for picking files allows specifying file formats that should be supported and visible via the dialog window.
Set dlgSaveAs = Application.FileDialog(msoFileDialogFilePicker)
dlgSaveAs.Filter = "Text Files (.txt)|*.txt|Word Documents (.docx)|*.docx|Word Template (.dotx)|*.dotx|All Files (*.*)|*.*"
dlgSaveAs.ValidateNames = true
Res = dlgSaveAs.Show
Okay, so I have been searching for ages to find this but no luck.
I am using:
Me.downloader.DownloadFileAsync(New Uri(fileUrl), Path.GetFileName(fileUrl), Stopwatch.StartNew)
To download a file but I want it to save to the root directory of my program in a file called launcher.
So for example, if my program is on the desktop and I open it and click start I want it to create the launcher folder if it's missing then download the files into that and if it's not then just download the files into it.
I've been looking everywhere to find code which would allow me to do this and I have tried lots of different things.
At the moment, it justs saves in the root directory of where the program is.
Thanks.
Try something like this:
Dim baseDir As String = AppDomain.CurrentDomain.BaseDirectory
Dim launcherDir As String = Path.Combine(baseDir, "Launcher")
If Not Directory.Exists(launcherDir) Then
Directory.CreateDirectory(launcherDir)
End If
Dim targetFile = Path.Combine(launcherDir, Path.GetFileName(fileUrl))
Me.downloader.DownloadFileAsync(New Uri(fileUrl), targetFile, Stopwatch.StartNew)
I'm using process.Start to run Convert.exe. This program's purpose is to convert all files which are in the exe's folder. So when I normally use it, I copy paste a file into the same folder as Convert.exe and then run Convert.exe. Convert.exe will create a new "converted" file in the same folder.
I'm trying to automate this tedious process. A User selects a file which needs to be converted from FolderA, I copy it to the same folder where Convert.exe is and I'm using process.start(Convert.exe) to run it.
Just to be clear, this "Convert.exe" accepts NO arguments.
The problem: "Convert.exe" is not converting the files in its folder. Instead it's converting all the files in FolderA for some weird reason. I don't know why it picked that folder, I never even try to send it as an argument or nothing.
Here's the code I have:
Dim techInfo As New System.IO.FileInfo(itm.strFilePath)
techInfo.CopyTo(ConverterPath & techInfo.Name)
Dim procInfoConvert As New ProcessStartInfo
procInfoConvert.CreateNoWindow = False
procInfoConvert.Arguments = ""
procInfoConvert.FileName = ConverterPath & "Convert.exe"
Dim procConvert As Process = Process.Start(procInfoConvert)
I did a test where I copy pasted a file into the "Convert.exe" folder and then just run this code:
process.start(ConverterPath & "Convert.exe")
The exe returns nothing, same as if there was no files in the folder.
The only thing I can think of is that when process.Start is run, it copies the file to another location and runs it from there.
Any ideas anyone?
Try this:
procInfoConvert.WorkingDirectory = ConverterPath
That'll set the process up to start in the directory it's contained in, instead of the current directory.