Question Wake up my NAS Question

MJCM

Member
Joined
Jun 17, 2004
Messages
22
Location
SE-Asia
Programming Experience
1-3
Hi Guys,

I have a small problem and I have no idea how to fix it.

I am copying files from time to time to my NAS (drive: Y:\) via Console Application which I want to Schedule.

My NAS sometimes goes to sleep (HDD stop spinning) after a period of not using it.

And when I try to copy files and my NAS is "asleep", sometimes the program get an error message.

Now I thought a solution for this problem is to wake up the NAS, so I create a TEMP File (with streamwriter) in the Root of the NAS Drive and then check if the File.Exists, this would wake the NAS up, but I have a strange issue.

See the following output

Attempt 1:

C:\Users\user\AppData\Local\Temp\test>test.exe
Directory to use is : C:\Users\user\AppData\Local\Temp\test
21/11/2017 18:13:19
Checking if Drive Y:\ is online ...
File Y:\tempfile.tmp deleted
21/11/2017 18:13:21
Drive Y:\ is available and online

When looking at my NAS, the drives are still "asleep" but the programma says it has created the file
and afterwards deleted it. All this in a mere 2 seconds, and I can't find a trace of the File (so it actually hasn't created and deleted it)

But Look at attempt 2: (started a couple seconds after attempt 1)

C:\Users\user\AppData\Local\Temp\test>test.exe
Directory to use is : C:\Users\user\AppData\Local\Temp\test
21/11/2017 18:13:26
Checking if Drive Y:\ is online ...
File Y:\tempfile.tmp deleted
21/11/2017 18:13:58
Drive Y:\ is available and online

After finishing this attempt the Drives are "awake" and you see it took 32 Seconds and not as attempt 1 only 2.
And the file was actually created and deleted.

The code I use is:

VB.NET:
Dim driveletter as String = "Y:\"
Dim tempfile As String = "tempfile.tmp"

            Try
                Using sw As StreamWriter = New StreamWriter(driveLetter & tempfile, False)
                    sw.Write("Dummy File")
                    sw.Close()
                End Using
                System.Threading.Thread.Sleep(1500)
                If File.Exists(driveLetter & tempfile) Then
                    File.Delete(driveLetter & tempfile)
                    Console.WriteLine("File " & driveLetter & tempfile deleted")
                    Console.WriteLine(Now)
                    Console.WriteLine("Drive " & driveLetter & " is available and online")
                Else
                    Throw New Exception
                End If
            Catch e As Exception
                Console.WriteLine(e.Message.ToString)
            End Try

Thanks for any pointers.

Ps: Not mapping the drive, but just wanna wake up the drive.
 
Last edited:
It seems to work ;)

Directory to use is : C:\Users\user\AppData\Local\Temp\test
22/11/2017 15:07:28
Checking if Drive Y:\ is online ...
File Y:\tempfile.tmp deleted
22/11/2017 15:07:44
Drive Y:\ is available and online

Now have to wait another 10 minutes (for the drives to go to sleep again) to test again ;)
 
Nope it doesn't work.

Directory to use is : C:\Users\<user>\AppData\Local\Temp\test
In NO Questions Asked Mode ...
22/11/2017 15:31:19
Checking if Drive Y:\ is online ...
File Y:\tempfile.tmp deleted
22/11/2017 15:31:21
Drive Y:\ is available and online

After Running, NAS is still asleep, and no error message.

Code is now this

Using sw As StreamWriter = New StreamWriter(driveLetter & tempfile, False)
 sw.Write("Dummy File")
 sw.Close()
                    Catch ex2 As Exception
                        Console.WriteLine("Error in Using")
                        Console.WriteLine(ex2.Message.ToString)
                    End Try
                End Using


Edit: Next Option ? Put it in a loop and let it run a couple of times ??
 
Last edited:
I now have done this.

Not very elegant (I must admit) and if someone knows a better trick please let me know, but after
running this the NAS is alive.

Using sw As StreamWriter = New StreamWriter(driveLetter & tempfile, False)
While x <> 10000000
sw.Write("Dummy File")
x += 1
End While
sw.Close()
Catch ex2 As Exception
Console.WriteLine("Error in Using")
Console.WriteLine(ex2.Message.ToString)
End Try
End Using

 
Don't use Using block if you need to know if exception occur, use Try-Catch instead. In your code the whole Using block is skipped if StreamWriter fails to create.
 
Oke I have re-written the Writer to this.


                Try
Dim writer As New IO.StreamWriter(driveLetter & tempfile, False)                    
writer.Write("dummy file")
                        writer.Close()
                    Console.WriteLine("File created")
                Catch ex As Exception
                    Console.WriteLine("Error {0}", ex.Message)
                End Try


Results: NO error Messages, File is written, BUT NAS is still not responsive. When I switch to the Drive (via Explorer) it takes at least another 30 seconds before I can switch dirs. So a solution would be to add the While statement. ???
 
Last edited:
Back
Top