Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an app that keeps track of all my DVDs. 
The DVDs are also digitized so they can be played right from the app. When I go into the app, I need to be sure that I am in the same drive as the digitized movies. So far, I have been just manually changing the drive but would really like to be sure it is in the correct drive from the beginning. 
I have tried the SetCurrentDirectory() command, but it doesn’t work. I do Imports System.IO 
This is the code:

<pre><pre>Imports System.IO
Imports System.Speech.Synthesis
Imports System.Runtime.InteropServices

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'DVD_ListDataSet.DVD_List' table.          You can move, or remove it, as needed.
        Me.DVD_ListTableAdapter.Fill(Me.DVD_ListDataSet.DVD_List)

        synth.SelectVoice("Microsoft Zira Desktop")
        If Directory.GetCurrentDirectory() <> "J:\" Then
            Directory.SetCurrentDirectory("J:\")
        End If

    End Sub
VB



The GetCurrentDirectory and SetCurrentDirectory commands are the only thing I can find to do this. What am I doing wrong????

Sorry for using wrong way to reply, did not realize I had to 'Update the question'. I promise to do better in the future.
I ask again, would this be a problem if everything I do when I'm running this program is in the same directory ("J:\")?? when I use OpenFile and SaveFile it is in "J:\"?

What I have tried:

I have tried not checking the drive and just setting it to the drive I want'
Directory.SetCurrentDirectory("J:\)"

but that didn't work either.
Posted
Updated 3-Jan-23 8:00am
v2
Comments
Graeme_Grant 3-Jan-23 14:21pm    
Did you read the comments below from Dave and myself about using a "fully qualified path"? Please read again as you have not applied those suggested changes.

Are you sure that it did not work? does the drive exist?

I did a quick check with a mapped drive on my system:
VB
Console.WriteLine($"Current: [{Directory.GetCurrentDirectory()}]")
Directory.SetCurrentDirectory(@"m:\")
Console.WriteLine($"Current: [{Directory.GetCurrentDirectory()}]")

and the output was:
Current: [C:\CodeProject Help\ChangeDrive\ChangeDrive\bin\Debug\net7.0]
Current: [m:\]
 
Share this answer
 
Graeme_Grant, you are correct, adding a few lines of code proved to me that the directory was changed. But when I run the program and the directory that appears is ALWAYS another directory and always the same directory.
So, first I changed the code in the Form_1 Load Sub to just:
Directory.SetCurrentDirectory("J:\")

Then I deleted the entire sub that actually plays the movie and rewrote it.
Original code:
Private Sub BtnPlay_Click(sender As Object, e As EventArgs) Handles btnPlay.Click
        MovieName = TitleTextBox.Text.Trim + ".mp4"
        If File.Exists(J:\ + MovieName) Then
            MovieName = TitleTextBox.Text
            Using sayitForm As New MovieScreen
                sayitForm.ShowDialog()
            End Using
        Else
            MessageBox.Show(TitleTextBox.Text & vbCrLf & vbCrLf & ", IS NOT AVAILABLE " & vbCrLf & "AT THIS TIME", "NOT FOUND", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

    End Sub


New Code:
Private Sub BtnPlay_Click(sender As Object, e As EventArgs) Handles btnPlay.Click
        MovieName = TitleTextBox.Text.Trim + ".mp4"
        If File.Exists(MovieName) Then
            MovieName = TitleTextBox.Text
            Using sayitForm As New MovieScreen
                sayitForm.ShowDialog()
            End Using
        Else
            MessageBox.Show(TitleTextBox.Text & vbCrLf & vbCrLf & ", IS NOT AVAILABLE " & vbCrLf & "AT THIS TIME", "NOT FOUND", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

    End Sub

As you can see the only change was to eliminate the reference to the drive "\J:" and everything works correctly. I am thoroughly confused as to why, but I ran with it.

darby
 
Share this answer
 
v2
Comments
Dave Kreskowiak 1-Jan-23 16:03pm    
You shouldn't be relying on what the "Current Directory" is. It WILL change if you use OpenFile or SaveFile dialogs.

What you should be doing is building fully qualified paths to your files using well-known starting paths, or, in your case, a specified root path.
Graeme_Grant 1-Jan-23 16:59pm    
As dave mentioned, you should be using a fully qualified path. eg:
Dim drive = "M:\"
Dim path = "New Music"
Dim subPath = "Top 20"

Dim qualifiedPath = IO.Path.Combine(drive, path, subPath)

will give you:
M:\New Music\Top 20

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900