Click here to Skip to main content
16,020,459 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: How to make a button that will copy two files and put them somewhere else PinPopular
Tom Deketelaere24-Aug-10 3:22
professionalTom Deketelaere24-Aug-10 3:22 
QuestionRe: How to make a button that will copy two files and put them somewhere else [modified] Pin
mshome7724-Aug-10 5:31
mshome7724-Aug-10 5:31 
GeneralRe: How to make a button that will copy two files and put them somewhere else Pin
spencepk25-Aug-10 2:42
spencepk25-Aug-10 2:42 
AnswerRe: How to make a button that will copy two files and put them somewhere else PinPopular
johannesnestler24-Aug-10 5:22
johannesnestler24-Aug-10 5:22 
GeneralRe: How to make a button that will copy two files and put them somewhere else Pin
mshome7724-Aug-10 7:40
mshome7724-Aug-10 7:40 
GeneralRe: How to make a button that will copy two files and put them somewhere else Pin
hahanottelling24-Aug-10 21:13
hahanottelling24-Aug-10 21:13 
GeneralRe: How to make a button that will copy two files and put them somewhere else Pin
johannesnestler24-Aug-10 22:28
johannesnestler24-Aug-10 22:28 
AnswerRe: How to make a button that will copy two files and put them somewhere else PinPopular
Tom Foswick24-Aug-10 21:58
Tom Foswick24-Aug-10 21:58 
Hi,

I won't tell you that you shouldn't do this or that your question is all wrong. I'll just try to give you an answer you can use.

There are a few nice built-in .NET methods for copying a file. Here are two:
IO.File.Copy(sourceFileName,destFileName)
My.Computer.FileSystem.CopyFile(sourceFileName,destinationFileName)

They both do the same thing, though I prefer the first example. The second example is not available if you are programming in C#, so it helps with portability not to use it.

Others have already warned you about the problems with hard-coding these paths and with possible/probable security problems in copying to your system directory

Now, with regards to your second question (running 392.exe) without knowing the path, the short answer is, "No", it is not possible.

The long answer is, "Yes", it is possible, but it is clunky. There are two strategies that I can think of. The first strategy involves environment variables, and someone else already talked about that. I wouldn't recommend that approach for a bunch of reasons.

The second approach involves searching for the file, then running it. The problems with this are:
- It is potentially quite slow. It could take a long time to find the file.
- It is potentially dangerous. If there was another file named "392.exe" that you didn't want to run, the code might find it and run it anyway.

However, the code for finding the file and running it would look something like this:

Private Function FindFile(ByVal fileName As String, ByVal searchDirectory As String) As String
    Dim fullPath As String = ""

    If Not IO.Directory.Exists(searchDirectory) Then Throw New IO.DirectoryNotFoundException

    fullPath = IO.Path.Combine(searchDirectory, fileName)
    If Not IO.File.Exists(fullPath) Then
        fullPath = ""
        Try
            For Each subDirectory As String In IO.Directory.GetDirectories(searchDirectory)
                fullPath = FindFile(fileName, subDirectory)
                If fullPath <> "" Then Exit For
            Next
        Catch uaException As UnauthorizedAccessException
            fullPath = ""
        End Try
    End If

    Return fullPath
End Function

This function will search all subdirectories to which your app has access for the file in question. You would use it like this:

myFile = FindFile("392.exe", "C:\")
If IO.File.Exists(myFile) Then Process.Start(myFile)


This would search all directories and subdirectories of C:\ for a file called "392.exe". If it finds it, it will return the full path to that file and set myFile to that path. Otherwise, it will return "", and myFile will be "". We check to see if myFile exists, and if so, we run it.

Now, as you can imagine, this could take a while. So, it is best not to unleash FindFile() on your C:\ root directory. Better to narrow it down a bit. .NET provides some really nice built-in functions for doing this. For example, if you just want to search all through the Program Files directory (and subdirectories), try:

myFile = FindFile("392.exe", Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles))


The function Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) will return the full path to the computer's Program Files directory, regardless of where that happens to be. You can reference other special folders in that same way. That is just one example.

Best of luck.
GeneralRe: How to make a button that will copy two files and put them somewhere else Pin
mshome7725-Aug-10 2:18
mshome7725-Aug-10 2:18 
AnswerRe: How to make a button that will copy two files and put them somewhere else Pin
Phan7om26-Aug-10 23:19
Phan7om26-Aug-10 23:19 
Questionget the IDLE time [Solved] Pin
εїзεїзεїз23-Aug-10 23:04
εїзεїзεїз23-Aug-10 23:04 
AnswerRe: get the IDLE time Pin
Nuri Ismail23-Aug-10 23:28
Nuri Ismail23-Aug-10 23:28 
GeneralRe: get the IDLE time Pin
εїзεїзεїз23-Aug-10 23:49
εїзεїзεїз23-Aug-10 23:49 
GeneralRe: get the IDLE time Pin
Nuri Ismail24-Aug-10 0:04
Nuri Ismail24-Aug-10 0:04 
QuestionHow to make the VB6 and MS-Access project Centralised? Pin
kokilag23-Aug-10 20:24
kokilag23-Aug-10 20:24 
AnswerRe: How to make the VB6 and MS-Access project Centralised? Pin
_Damian S_23-Aug-10 20:41
professional_Damian S_23-Aug-10 20:41 
GeneralRe: How to make the VB6 and MS-Access project Centralised? Pin
kokilag24-Aug-10 19:31
kokilag24-Aug-10 19:31 
GeneralRe: How to make the VB6 and MS-Access project Centralised? Pin
_Damian S_24-Aug-10 20:38
professional_Damian S_24-Aug-10 20:38 
GeneralRe: How to make the VB6 and MS-Access project Centralised? Pin
kokilag25-Aug-10 18:36
kokilag25-Aug-10 18:36 
GeneralRe: How to make the VB6 and MS-Access project Centralised? Pin
kokilag27-Aug-10 0:37
kokilag27-Aug-10 0:37 
QuestionMessage Removed Pin
23-Aug-10 11:37
kereljansen23-Aug-10 11:37 
AnswerRe: sql string Pin
Wes Aday23-Aug-10 12:32
professionalWes Aday23-Aug-10 12:32 
AnswerRe: sql string Pin
Luc Pattyn23-Aug-10 12:39
sitebuilderLuc Pattyn23-Aug-10 12:39 
GeneralRe: sql string Pin
Richard A. Dalton23-Aug-10 13:31
Richard A. Dalton23-Aug-10 13:31 
AnswerRe: sql string Pin
Luc Pattyn23-Aug-10 13:47
sitebuilderLuc Pattyn23-Aug-10 13:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.