Page 1 of 1

How to check USB keydisk is inserted?

Posted: Tue Jun 07, 2016 3:19 pm
by ianrobo75
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).

Re: How to check USB keydisk is inserted?

Posted: Fri Jun 10, 2016 5:05 am
by liestol
Hi,

Try defining the following bool:

Code: Select all

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).

Re: How to check USB keydisk is inserted?

Posted: Mon Jun 13, 2016 3:03 pm
by ianrobo75
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: Select all

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?

Re: How to check USB keydisk is inserted?

Posted: Tue Jul 05, 2016 8:51 am
by Claus Nielsen
Try

Code: Select all

// 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