0

I´m trying to open a file named the same as a bar code but I get the error that the file cannot be found?

I use this line to read the file:

string[] DxfFile = System.IO.File.ReadAllLines(textBoxBarCodeScanner.Text);

It works fine and open the file correctly if I assign the text box with:

textBoxBarCodeScanner.Text = (@"PLANKA.DXF");

I use these lines to read from the serial port:

RecievedData = RecievedData.Replace("\r\n", "").Replace("\r", "").Replace("\n", "");
textBoxBarCodeScanner.Text = (@RecievedData);

I had to remove /r first but it did not help. I´m kind a beginner so I´m not so good at finding the right debug information for you so here is what I got and please tell me where I found more useful information. If I break at the exection and look at "locals" I have a row that says text, and there I got "PLANKA.DFX" which seem to be correct.

Debug error message is as follow:

Additional information: Could not find file E:\Win7\Google Drive\Visual Studio 2013 Projects\Husmaskin GUI\Husmaskin GUI\bin\Debug\PLANKA.DFX.

This works (@"PLANKA.DXF"): Working textbox

This does not (@RecievedData)?? Not working textbox

John M. Wright
  • 4,477
  • 1
  • 43
  • 61
JeeyBee
  • 21
  • 1
  • 8
  • So, is the file actually located at the path shown in the error message? If not, where is it located? – Heinzi Dec 16 '17 at 13:16
  • Yes, it is and its the same path as the code executes in, and as it work with just putting textBoxBarCodeScanner.Text = (@"PLANKA.DXF"); in the textbox. Could it be that it adds something that is not shown? – JeeyBee Dec 16 '17 at 14:40
  • Is it possible in some way to get the "raw" string out and compare them? – JeeyBee Dec 16 '17 at 14:51

1 Answers1

0

In your screenshots, you have two different values for the file extension: DXF and DFX.

There may be extra whitespace around the incoming text, so I would suggest also adding .Trim() to your code.

I would also suggest checking for the file in your code and creating an error message that is more useful to you:

var filename = txtFoo.Text;
if (!File.Exists(filename))
     throw new Exception($"Could not find file '{filename}'");
John M. Wright
  • 4,477
  • 1
  • 43
  • 61