Results 1 to 8 of 8

Thread: Wake up my NAS Question

  1. #1
    .NET Framework
    .NET 4.5
    Join Date
    Jun 2004
    Location
    SE-Asia
    Posts
    22
    Reputation
    176

    Wake up my NAS Question

    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:

    Code:
    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 by MJCM; 11-22-2017 at 2:21 AM.

  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,209
    Reputation
    2859
    'Using' code block is like a Try-Catch-Finally where you don't catch the exception, see Using Statement (Visual Basic) | Microsoft Docs
    [xcode=vb] code here [/xcode] - see bbcode list or use formatting buttons in posting editor.

    Visual Studio Community 2017

  3. #3
    .NET Framework
    .NET 4.5
    Join Date
    Jun 2004
    Location
    SE-Asia
    Posts
    22
    Reputation
    176
    Thx, so an extra catch in the Using block !

    Will try that and report back.

  4. #4
    .NET Framework
    .NET 4.5
    Join Date
    Jun 2004
    Location
    SE-Asia
    Posts
    22
    Reputation
    176
    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

  5. #5
    .NET Framework
    .NET 4.5
    Join Date
    Jun 2004
    Location
    SE-Asia
    Posts
    22
    Reputation
    176
    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 by MJCM; 11-22-2017 at 3:45 AM. Reason: Added Edit

  6. #6
    .NET Framework
    .NET 4.5
    Join Date
    Jun 2004
    Location
    SE-Asia
    Posts
    22
    Reputation
    176
    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


  7. #7
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,209
    Reputation
    2859
    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.
    [xcode=vb] code here [/xcode] - see bbcode list or use formatting buttons in posting editor.

    Visual Studio Community 2017

  8. #8
    .NET Framework
    .NET 4.5
    Join Date
    Jun 2004
    Location
    SE-Asia
    Posts
    22
    Reputation
    176
    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 by MJCM; 11-22-2017 at 5:26 AM.

Similar Threads

  1. Question Wake up event
    By Zexor in forum VS.NET General Discussion
    Replies: 1
    Last Post: 08-22-2017, 4:43 AM
  2. Wake up failing
    By DavyEFC in forum Windows Forms
    Replies: 2
    Last Post: 05-03-2016, 10:46 AM
  3. Question How to Wake the computer from Sleep?
    By ImDaFrEaK in forum Non-GUI
    Replies: 5
    Last Post: 07-23-2009, 10:20 AM
  4. Question Wake Printer
    By jshultz in forum Reporting / Printing
    Replies: 0
    Last Post: 06-04-2009, 3:26 PM
  5. a data access basic question and a calculator question
    By newbeginner in forum VB.NET General Discussion
    Replies: 1
    Last Post: 11-08-2007, 10:48 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •