473,508 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Opening Devices in C# -- Why am I getting "Illegal Characters in Path" exceptions?

I'm trying to communicate with a USB device using C#. I'm able to determine
the device path using P/Invoke and SetupDiGetClassDevs,
SetupDiEnumDeviceInterfaces, and SetupDiGetDeviceInterfaceDetail, but I
can't open the device using File.Open. I get the following error:

An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll

Additional information: Illegal characters in path.

The path name is
\\?\usb#vid_0da0&pid_1000#6&2d560690&0&1#{6d2f6bb8-73e9-4283-a478-b6b493962df9}

The path name is determined by code, so it shouldn't be a problem with not
properly escaping the slashes. Even double- and triple-escaping the slashes
doesn't do any good. There aren't any hidden null characters in the string
either.

Dumping System.IO.Path.InvalidChars produces: (non-printable chars removed)

System.IO.Path.InvalidPathChars
{Length=0xf}
[0x0]: 0x22 '"'
[0x1]: 0x3c '<'
[0x2]: 0x3e '>'
[0x3]: 0x7c '|'
[0x4]: 0x0 ''
[0x5]: 0x8
[0x6]: 0x10
[0x7]: 0x11
[0x8]: 0x12
[0x9]: 0x14
[0xa]: 0x15
[0xb]: 0x16
[0xc]: 0x17
[0xd]: 0x18

Any ideas why .net doesn't like this? Looking at the stack trace, my hunch
is that it's trying to split the file name into the directory and file name
parts, and \\?\ isn't a "real" directory. Is there a way to open this device
without resorting to using P/Invoke and CreateFile/ReadFile/WriteFile?

Thanks,
Karl Koscher
Nov 16 '05 #1
2 10814
Is there a way to open this device
without resorting to using P/Invoke and CreateFile/ReadFile/WriteFile?

No, the FCL are designed to open logical files only. So you will have to
PInvoke CreateFile and use the file handle returned to access the device
using the FileStream class.

Willy.
Nov 16 '05 #2
> No, the FCL are designed to open logical files only. So you will have to
PInvoke CreateFile and use the file handle returned to access the device
using the FileStream class.


Thanks! I wasn't aware that FileStream could use native file handles. It
works fine now.

- Karl
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1695
by: BenO | last post by:
Hi I'm new to python and need to write a function to replace certain characters in a string (html). The characters I need to replace come from MS Word copy & paste and are: ' (Left quote)...
10
2161
by: Alpha | last post by:
I use FolderBrowserDlg for user to select a folder path then store it in the sql table. I then retrieve it to concatenate in a sqlcommand text to retrive files from that directory but it won't...
1
5393
by: Steve Grahovac | last post by:
I have been having trouble the last few days opening any ASP.NET web forms in the design view in the designer. Every time I clicked on an aspx file I got a message box with the error "Unable to...
9
13663
by: Clinton Frankland | last post by:
Hi, On a Windows 2000 Server when attempting to use System.IO.Directory.CreateDirectory(string.concat(Server.MapPath(""), "\verify")) I receive a System.IO.DirectoryNotFoundException error:...
2
5140
by: Guoqi Zheng | last post by:
Dear sir, I am trying to save a binary data by below script, however,it always give me an error of "Could not find a part of the path "C:\temp\". " Any idea what I did wrong here? ...
3
15763
by: Eckhard Schwabe | last post by:
I only found one post on Google where someone mentions the same problem with a DataSet: XmlDataReader in .Net 1.1 can not read XML files from a path which contains "%10" or "%3f". code to...
1
2313
by: Shilpa | last post by:
Hi, I have a OpenFileDialog on my windows form whose filter is *.*. I want the users to be able to further filter the files by giving *.doc or *.zip etc in the "file name" field of the dialog...
9
4002
by: larrybud2002 | last post by:
I've read about this error in this group and others from early 2006 but without any resolution, so I thought I'd bump it up to see if there's a solution. Trying to build a website on ...
6
4572
by: =?Utf-8?B?UGF1bA==?= | last post by:
I am getting an "Access to the path "xxxx" is denied error. I believe is because the file that I am writing to programatically is being read/written to by another end user. These files have the...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.