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"):
This does not (#RecievedData)??
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}'");
Related
I have the following code in C#:
FileInfo file = new System.IO.FileInfo(filePathAndName);
file.Directory.Create();
File.WriteAllText(filePathAndName, headers + singleOption.Value);
When the file name is
D:\Option Data\DiscountOptionData\Single Option Files\NUL\NUL2008-09-20p32.50.csv
then it throws a System.IO.DirectoryNotFoundException. In fact, any with NUL in them throw the exception.
But any other file names I'm using, such as
D:\Option Data\DiscountOptionData\Single Option Files\NSI\NSI2006-06-17c50.002006-02-09.csv
do not throw exceptions.
The relevant parts of the filename are read from an excel file. I've tried using LEN([Cell]) on them and can't find any hidden characters.
You probably shouldn't use nul (or con or prn) for file names, since these are considered special devices by windows. You can see this if you open up a cmd window and enter type nul:
c:\pax> type nul
c:\pax> type nulx
The system cannot find the file specified.
In fact, it even misbehaves with an extension. I once had a source file const.h which I renamed to con.h and then spent a bit of time wondering why my code wasn't compiling. Turns out, because I'd used #include "con.h", Windows had
helpfully opened up the console device to read my header file.
In other words, it was waiting for me to type it in :-)
If you must have a directory hierarchy where NUL*.csv files are stored, one solution is to use a prefix to ensure special device names don't appear, something like:
D:\OptnData\DscntOptnData\SnglOptnFls\dirNUL\NUL2008-09-20p32.50.csv
^^^^^^
This bit
I have verified multiple times that the file exists, and when I copy-and-paste the path to the file from the code into Windows explorer, it works. It takes me to the file. So please help, what is going on? The exception message, for your reference says
System.IO.FileNotFoundException: Could not find file "/C:\Users\Daniel\Desktop\Couryah\products_export.csv
Why is this forward slash in front of it? Is it the culprit? How do I fix it?
public void PopulateList()
{
string line;
System.IO.StreamReader file = new System.IO.StreamReader("C:\\Users\\Daniel\\Desktop\\Couryah\\products_export.csv");
while ((line = file.ReadLine()) != null)
{
Product newProduct = new Product();
newProduct.setAll(line.Split(',')[1], line.Split(',')[3], Convert.ToDouble(line.Split(',')[19].Replace("$", "")), line.Split(',')[24]);
productList.Add(newProduct);
}
}
I would pre-check to make sure the file exists. Put into a variable just to be sure no false/bogus value with bad forward slash. Then, check to see if the file exists or not to confirm BEFORE opening.
Next, instead of stream, I just changed to a File.ReadAllLines() which reads each line at a time into an array.
public void PopulateList()
{
var whatFile = "C:\\Users\\Daniel\\Desktop\\Couryah\\products_export.csv";
if ( ! System.IO.File.Exists( whatFile ))
{
MessageBox.Show("No such file: " + whatFile);
return;
}
foreach( var line in System.IO.File.ReadAllLines( whatFile ))
{
Product newProduct = new Product();
newProduct.setAll(line.Split(',')[1], line.Split(',')[3], Convert.ToDouble(line.Split(',')[19].Replace("$", "")), line.Split(',')[24]);
productList.Add(newProduct);
}
}
Now, this does not necessarily answer why a file does not exist, but here is one thing I specifically ran into and was scratching the heck out of my head. I had a similar for a client. I had a file on the desktop that was a csv, but no matter what, had same issue.
What I actually had found is the file "MyFile.csv" was ACTUALLY a "MyFile.csv.txt". Wondering what the... Ended up going to a DOS command prompt, changed to folder and did a DIR of that folder to see it.
Come to find, Windows likes to default HIDING FILE NAME SUFFIX of common file extensions. So, even though the desktop had .csv, because the .txt was hidden was causing the not found file. To confirm, I changed the file manager to NOT HIDE file name suffix. Sure enough, stripped the .txt and everything worked.
Not saying this is what you have, but sometimes the obvious isn't.
One more note. I would put a breakpoint in your debugger to stop at the point of checking the file. Then use your watch window and see what variables are and also check/change as needed to confirm its doing what you thought it should.
One additional option...
Try using what WINDOWS thinks is your desktop.
var whatFile2 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Couryah\\products_export.csv")
How does THIS "SpecialFolder.Desktop" compare with your C:\users...
Basically, I'm building a website that allows user to upload file.
From the front end (JavaScript), the user will browse a file, I can get the site to send POST data (the parameter "UploadInput" and it's value, which the value is the file)
In the backend (C#), I want to make a copy of the file and save it in a specific path.
Below is the way I did it.
var files = Request.Files;
file[0].SaveAs("\temp\\" + file[0].FileName);
The problem I ran into is that I got the error message saying index out of range. I tried Response.Write(files.Count) and it gives me 0 instead of 1.
I'm wondering where I did wrong and how to fix it, or if there's a better way of doing it.
Thanks!
Edit:
I am using HttpFox to debug. From HttpFox, I can see that under POST data, parameter is "UploadInput" and the value is "test.txt"
Edit 2:
So I tried the way Marc provides, and I have a different problem.
I am able to create a new file, however, the content is not copied over. I tried opening the new created file in notepad and all it says is "UploadInput = test.txt"
If they simply posted the file as the body content, then there will be zero "files" involved here, so file[0] will fail. Instead, you need to look at the input-stream, and simply read from that stream. For example:
using(var file = File.Create(somePath)) {
Request.InputStream.CopyTo(file);
}
My text file named test.txt contains
Fixing the type manager error during checkin of the elements and supporting issues during Checkin/Checkout & merge related problems.
Now i want to remove the text "supporting issues" using c#.
Please anybody let me know.
Thanks in advance,
Naveenkumar.T
if the file is fairly small you can do this:
using System.IO; // at the top of the file
string file = "path.txt"; // change to the path
// read the whole file into a string, make a replacement, then write all string to a file
File.WriteAllText(file, File.ReadAllText(file).Replace("supporting issues ",""));
Hope that helps.
Hey this is going to be one of those dumb questions. I am trying to pick up a file on my local system and I keep getting a FileNotFoundException thrown.
Someone set me straight please :)
if( File.Exists(#"C:\logs\hw-healthways-prod_2009-08-26.tar"))
{
Console.WriteLine("Yay");
}
else
{
throw new FileNotFoundException();
}
Tried moving the file into the same location as the executing application and did the following:
if( File.Exists("hw-healthways-prod_2009-08-26.tar"))
Same thing.
Then I made a random txt file and parked it there too.. "me.txt"
And it works?! So you thing the file name is the problem?
Try doing Directory.GetFiles(#"C:\logs"). It's possible that the file in question has odd characters that are getting interpreted one way by Windows Explorer (presumably where you're reading "the file's property" from?) but a different way by the .NET Framework. This can happen if you have UTF-8 characters in the filename (perhaps an en dash?).
May be the name of the file is "hw-healthways-prod_2009-08-26.tar.tar" instead of "hw-healthways-prod_2009-08-26.tar", I had this problem because by default the extension files are hidden on windows
You may want to check your file permissions. Your computer may not have permission to the file.
C:\logs\hw-healthways-prod_2009-08-26.tar should be C:\\logs\\hw-healthways-prod_2009-08-26.tar. \ means the next character is an escape character.