VB check inside a folder to see if there are files - vb.net

I am trying to get my vb.net application to look inside a folder on the web server and then let me know whether there are files in there or if the folder is empty...Would anybody know where i would begin? Thanks

Use the DirectoryInfo.EnumerateFiles() method.
Dim myDir as DirectoryInfo = new DirectoryInfo(pathToDir)
If (myDir.EnumerateFiles().Any())) Then
' Got files in direcotry!
End If
If you are also interested in finding out if there are directories within this one, there is also DirectoryInfo.EnumerateDirectories().

I would suggest you have a look at Directory.GetFiles

If your program is running on the web server, you can simply check whether Directory.GetFileSystemEntries(path) returns anything.
If your program is running on a client, you'll need to make a server-side script that calls Directory.GetFileSystemEntries and returns a value to the client.

Related

C++/CLI StreamWriter cannot specify path

i' m trying to make loginapp in c++/cli with users data log. I would use fstream but it isnt possible in cli. So I use StreamWriter and i cant specify totally normal path C:\log.txt (also tried) C:\\log.txt And when i debug my programme i get error 'access is denied' but also i have this problem when i run it like an admin. However when I just specify it like a log.txt it works and the file is being maked in vs project folder and its not satisfying for me. I paste my code below.
StreamWriter^ log = gcnew StreamWriter("C:\log.txt", true);
log->WriteLine(newLogin);
log->WriteLine(newPassword);
log->Close();
label7->Visible = true;
I also tried a method with # operator but it also doesnt work. Sorry for my english.
C:\ is a path with admin permissions required. Try launching Visual Studio as administrator or change your path to some other which does not need them.
The documentation seems to say that the StreamWriter constructor will only accept a filename, not a full path: https://msdn.microsoft.com/en-us/library/19czdak8.aspx
The functionality that you want is available with another class, namely System.IO.File: https://learn.microsoft.com/en-us/dotnet/api/system.io.file?view=netframework-4.7.1

Open file with VB.NET application (How does my project know which file is being opened?)

I am in the process of making a simple image viewer in VB.NET. I want to be able to open an image with my application. How does VB.NET receive the URL of the file that the user is trying to open?
Seems simple but without knowing key terms, my Google searches are returning completely the wrong things!
You want to inspect the Environment.CommandLine Property
This property provides access to the program name and any arguments specified on the command line when the current process was started.
The program name can include path information, but is not required to do so. Use the GetCommandLineArgs method to retrieve the command-line information parsed and stored in an array of strings.

Self updating application in vb.net

I want my application to self-update if there is a different size executable available on the remote server. The problem I got is that when I kill the process to replace the application executable, nothing more happends - nothing more is executing after the eprocess.Kill() even though I am trying to freeze the thread during the file replacement process. Is there something I am doing wrong?
Here is my code:
Dim Request As System.Net.WebRequest
Dim Response As System.Net.WebResponse
Dim FileSize As Integer
Request = Net.WebRequest.Create("http://mywebsite.com/File.exe")
Request.Method = Net.WebRequestMethods.Http.Get
Response = Request.GetResponse
FileSize = Response.ContentLength
Dim mySize As New IO.FileInfo(Application.ExecutablePath)
If FileSize <> mySize.Length Then If File.Exists(tempPath & "\File_tmp.exe") Then
File.Delete(tempPath & "\File_tmp.exe")
End If
Patcher.DownloadFileAsync(New Uri("http://mywebsite.com/File.exe"), tempPath & "\File_tmp.exe") 'Patcher is defined before, you might think that its not working, but this is just a piece of code, and the new file is downloading properly. The described problem is
While Patcher.IsBusy
Threading.Thread.Sleep(100)
End While
Do While True
For Each eprocess As Process In Process.GetProcesses
If eprocess.ProcessName = "MyApplication" Then
eprocess.Kill()
End If
Next
File.Delete(Application.ExecutablePath)
'Copy downloaded file to the application executable path directory
File.Copy(tempPath & "\File_tmp.exe", Application.ExecutablePath)
Threading.Thread.Sleep(30) 'freeze thread...
Loop
End If
One way to handle this is make a separate(smaller exe) in your project that handles the downloading of the new version. First it would close the current version and since it's not tied to the original app the uploader app is still running, it downloads, then installs it and then launches the new version.
There is built in functionality to do this for you.
Have a look at ClickOnce. This negates the need to have a separate application to do the updating for you and allows you to specify a minimum and recommended version, whether the app checks for a new version before or after it starts, etc.
Some more links that may be of use:
http://msdn.microsoft.com/en-us/magazine/cc163973.aspx
http://www.codeproject.com/Articles/38546/Click-Once-Deployment-Technique
I say you write a function that creates a batch file which
downloads the new version of your exe
closes your app and overwrites your exe with the new.
Opens your app
Then your app on startup should look for a such batch file and erase it.
Make an updater whose work will be downloading the files from the remote server then copies/moves them into your main application directory.
I've been using this approach for a while now and it works fine.
Maybe this approach here will work for you https://www.codeproject.com/Articles/35787/VB-NET-Background-File-Downloader
Thought i would add my suggestion to the mix being that this is still the top result in google for stackoverflow.
Firstly.. I can not stand the ClickOnce Solution as it modifies to much of the project and requires other steps and so on.. So i ruled that out after 10 minutes of looking into it.
Also as for having another exe to handle the updates..This was not ideal for me either... So i had my main exe/setup install do all this for me.
Basically i have a website where i host the setup install files with a simple JSON data file to hold the version information. This is accessed via the application itself using a http request and json decoding to query the latest version and compare it against the apps current version.
If a new version is found, the application will download a copy of the new install and place it inside of the applications main directory in a temp folder/installers folder.
Once the download has completed, i execute the installer from within the application and dont waitforexit. The next like of code will trigger the applications save & exit functions.
At this point you have the install running but the main application has exited.. From here the installer can continue as normal and start the application on complete.
You can of course vary alot of things here like silent installs and so on.. but using this method, it allows me to get away with just the normal installer and app exe..

System.IO.Directory.Getfiles Silverlight 4 is not working

I'm using Silverlight 4 OOB & elevated trust.
I need to get all the filenames on specific directory & populate an object List(Of String)
The compiler throws an error "Method not found" on .Getfiles() method.
Dim files() As String = System.IO.Directory.Getfiles(Path) 'this line is failing..
Help!
The GetFiles is marked as "Security Critical" and therefore cannot be used from your code.
You will want to use the EnumerateFiles method instead. GetFiles is sooo .NET 1.0, EnumerateFiles is much slicker, even in the full framework you'd want avoid this older Array returning API if you can.
As far as I know you cannot directly access the whole hard drive using Silverlight OOB.
Quoting from Silverlight site:
When running in a trusted environment, you can access only files in
user folders, specifically the MyDocuments, MyMusic, MyPictures, and
MyVideos folders. Although this makes sense from a security point of
view, it’s limiting. You want to enable the user to drag their data
from any location. As it stands right now, if you try to drop a file
from a location other than stated above, Silverlight will throw a
security error.
Please refer to this link for details on how to work with the hard drive using Silverlight OOB:
http://www.silverlight.net/learn/overview/out-of-browser-applications/advanced-silverlight-out-of-browser-introduction#Exercise3

Can a loaded DLL know where it is located?

I am building a DLL that is used by Office. When Office runs with it, I would like to identify where it is located. Is that possible?
ex. of code within the DLL when it is run within Office:
// should return C:\tmp\officeaddin.dll,
// currently C:\Program Files\Microsoft Office\Office 12
MessageBox.Show(Application.StartupPath)
The following should work even if running inside of Office:
Getting the path of the current assembly
(It basically boils down to Assembly.GetExecutingAssembly().Location, but see the link above for more detailed information.)
If it is a .NET library, you should use Assembly.GetExecutingAssembly().Location.
Application.StartupPath shows the path to the main app.
This works for me:
Environment.CurrentDirectory
So you could just set a string to it such as:
Dim location as String = Environment.CurrentDirectory & "\"
When the DLL loads, It calls DllMain with the instance handle. If you implement this function, you can then record the instance handle of the DLL. From this, you can then call GetModuleFileName.
In .NET this is taken care of for you. See this question and selected answer for details.