Welcome Guest ( Login | Register )



All times are UTC - 7 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: How to check USB keydisk is inserted?
PostPosted: Tue Jun 07, 2016 3:19 pm 

Joined: Mon Sep 22, 2014 6:15 pm
Posts: 12

Offline
I'm trying to check that there is a USB keydisk is inserted.

I've tried the following script in a function and it always returns false whether the USB keydisk is inserted or not...
try
{
if (Directory.Exists("\\Hard Disk")
{return true;}
else
{return false;}
}
catch
{return false;}

I've also tried the above but using "\\HardDisk" instead of "\\Hard Disk", same result.

I've also tried writing to a file "\\HardDisk\Test.Txt" & "\\Hard Disk\Test.Txt" , and this never faults, so returns true in my function, but doesn't actually write to the USB keydisk.

So what am I doing wrong? Is there another way to check that a USB keydisk is inserted - how do I do that?

I'm using a iX T7F-2 currently, but have the same problem on an iX T7A.
I've tried with FTP enabled and disabled for the USB, same result.
I can see the ftp share for the USB keydisk, when enabled (ftp://<IP address of iX>/HardDisk).


 Profile  
 
 Post subject: Re: How to check USB keydisk is inserted?
PostPosted: Fri Jun 10, 2016 5:05 am 

Joined: Thu Feb 11, 2016 5:11 am
Posts: 10

Offline
Hi,

Try defining the following bool:

Code:
bool UsbPresent = Directory.Exists("\\Hard Disk");


Should return false/true depending on USB presence. It's using System.IO.

You're missing a closing parentheses in your if statement, that could be why you're always getting false (as the try fails and the catch returns false).


 Profile  
 
 Post subject: Re: How to check USB keydisk is inserted?
PostPosted: Mon Jun 13, 2016 3:03 pm 

Joined: Mon Sep 22, 2014 6:15 pm
Posts: 12

Offline
Thanks for the reply.

The missing closing parentheses was only when typing the code into here, not in the application.
I tried bool UsbPresent = Directory.Exists("\\Hard Disk");, as you suggested and no joy. In fact, this time the USBPresent function always returns true, whether the USB is inserted or not, and the Write to USB always returns false.

Here's the actual code I have...
Code:
namespace Neo.ApplicationFramework.Generated
{
    using System.Windows.Forms;
    using System;
   using System.IO;
    using System.Drawing;
   using System.Text;   
    using Neo.ApplicationFramework.Tools;
    using Neo.ApplicationFramework.Common.Graphics.Logic;
    using Neo.ApplicationFramework.Controls;
    using Neo.ApplicationFramework.Interfaces;
   
   
   public partial class Screen1
   {
      private string USBpath = "\\Hard Disk";
      
      void TestInsertedExists_Click(System.Object sender, System.EventArgs e)
      {
         if (USBExists() == true)
         {PicInsertedExistsYes.Visible  = true;}  //show Tick
         else
         {PicInsertedExistsNo.Visible  = true;}   //show cross
      }
      
      void TestNotInsertedExists_Click(System.Object sender, System.EventArgs e)
      {
         if (USBExists() == true)
         {PicNotInsertedExistsYes.Visible  = true;}
         else
         {PicNotInsertedExistsNo.Visible  = true;}
      }
      
      void TestInsertedWrite_Click(System.Object sender, System.EventArgs e)
      {
         if (writetoUSB() == true)
         {PicInsertedWriteYes.Visible  = true;}
         else
         {PicInsertedWriteNo.Visible  = true;}
      }
      
      void TestNotInsertedWrite_Click(System.Object sender, System.EventArgs e)
      {
         if (writetoUSB() == true)
         {PicNotInsertedWriteYes.Visible  = true;}
         else
         {PicNotInsertedWriteNo.Visible  = true;}
      }

      

      private bool USBExists()
      {
         //AT THE MOMEMNT THIS ALWAYS RETURNS TRUE WHETHER KEYDISK INSERTED OR NOT
         try
         {
            bool UsbPresent = Directory.Exists(USBpath);
            if (UsbPresent = true)
               {return true;}
            else
               {return false;}
         }
         catch
            {return false;}
      }
      
      private bool writetoUSB()
      {
         //AT THE MOMEMNT THIS ALWAYS FALSE TRUE WHETHER KEYDISK INSERTED OR NOT
         // Create a temporary file, and put some data into it.
         try
         {
            string path = USBpath + "\\WriteTest.Txt";
            using (FileStream fs = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None))
            {   // Add some information to the file.
               Byte[] info = new UTF8Encoding(true).GetBytes("test write to SD");
               fs.Write(info, 0, info.Length);   
               fs.Close();
            }
            return true;
         }
         catch
            {return false;}
      }
      
   }
}


So, any more suggestions?


 Profile  
 
 Post subject: Re: How to check USB keydisk is inserted?
PostPosted: Tue Jul 05, 2016 8:51 am 

Joined: Sun May 31, 2015 2:38 am
Posts: 14

Offline
Try

Code:
// Check if a USB drive is present.
bool UsbPresent = Directory.Exists("\\FlashDrive");
// Check is a SD Card is present.
bool SDCardPresent = Directory.Exists("\\Storage Card");


Works on a T7A


 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 7 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: