san1850

Hi, Currently iam scanning data from a barcode and asking users to enter some params on the form.Then i need to get the data entered and need to store it to text file in the device.If i create a text file in the scanner manually and scan the barcode it saves the way i wanted.My problem is if the txt file does not already exist on the scanner i need to add it to the scanner programatically.

string MyFilePath = "\\Program Files\\Document1.txt";

if (!File.Exists(MyFilePath))

File.Create(MyFilePath);

try

{

StreamWriter sw = new StreamWriter(MyFilePath);

sw.WriteLine(Data);

sw.Flush();

sw.Close();

}

I used the underlined portion of code to create a text file but it is not working.

And the other problem if the user scanned a barcode entered parms and then hit the enter button it stores perfectly.But if he does a scan again and hit enter to store the next entry the text file is not adding the second entry it is instead updating the first entry..

Please throw me some light on solving these two issues.Thank you.



Re: Smart Devices VB and C# Projects Adding a text file in the mc1000 scanner

Christopher Fairbairn

Hi,

I am not sure why your code is not creating the file file, your code sample definatly works for me, and I can not see anything wrong with it.

Code Snippet

string MyFilePath = "\\Program Files\\Document1.txt";

if (!File.Exists(MyFilePath))

File.Create(MyFilePath);

With respect to your second problem, try using something similiar to the following code sample:

Code Snippet

using System.IO;

using (TextWriter tw = new StreamWriter("\\test.txt", true))

{

tw.WriteLine("Hello this is another line");

}

The StreamWriter has an overloaded constructor. The version I haved used above accepts an additional boolean parameter which determines how it should behave if the specified file already exists (should it append to it, or overwrite it).

The default behaviour is to overwrite an existing file, by passing 'true', you specify that you want the StreamWriter to append to any existing content.

Hope this helps,

Christopher Fairbairn,






Re: Smart Devices VB and C# Projects Adding a text file in the mc1000 scanner

quest3

Hi thanks for ur reply, now iam able to create a file, if does not exist and data is appending after i specified true attribute in streamwriter.But i kind of ran into an other problem.The problem is, when the file is not existing it is creating the txt file but it is not appending data at same time instead it showing me the exception as

System.IO.IOException: An error message cannot be displayed because an optional resource assembly containing it cannot be found

It is creating the file in specified patha and throwing me the above error.But once the file created next time i run the application and execute the same event again it is appending data.Its weird that it is unable to create and append the data to txtfile at same time.I was able to do this in webapp but i dont know what iam missing here.


if (!File.Exists(MyFilePath))

File.Create(MyFilePath);

try

{

StreamWriter sw = new StreamWriter(MyFilePath,true);

sw.WriteLine(Data);

sw.Flush();

sw.Close();

}

catch (Exception ex)

{ }





Re: Smart Devices VB and C# Projects Adding a text file in the mc1000 scanner

Christopher Fairbairn

Hi,

I can't see anything wrong with your code sample. Are you able to provide a small sample project which causes the exception to occur. Your code sample does not throw an exception for me (if I add a line at the top of it such as:

string MyFilePath = "\test.txt";

With respect to the exception message stating an optional resource assembly is not present. You need to install an additional CAB file onto your device. See http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=119204&SiteID=1 for further details.

Hope this helps,

Christopher Fairbairn.






Re: Smart Devices VB and C# Projects Adding a text file in the mc1000 scanner

quest3

Hey it works now, i dont how it is creating but txt is created by commenting the create file method as shown below

//File.Create(MyFilePath);

try

{

StreamWriter sw = new StreamWriter(MyFilePath,true);

sw.WriteLine(Data);

sw.Flush();

sw.Close();

}

catch (Exception ex)

{ }

I dont know how exactly it is working but it creating automatically i tried it like 10 times to make sure, to my surprise it is working.Thanks for ur help.





Re: Smart Devices VB and C# Projects Adding a text file in the mc1000 scanner

Christopher Fairbairn

Hi,

I am pleased that you have found a solution.

With respect to why your code snippet with a call to File.Create() causes an IOException:

If you modify your creation logic to be as follows:

Code Snippet

if (!File.Exists(MyFilePath))

{

Stream s = File.Create(MyFilePath);

s.Close();

}

You should see that the IOException disappears. The File.Create method creates a file and then returns a Stream that allows you to access the file. If you don't explictly close the stream, other parts of your code will not be able to open the same file, due to this stream having an exclusive access lock open on the file. By explictly closing the stream, we are releasing the file, which enables the StreamWriter to successfully open up the same file.

If these two code segements were in different parts of an application, you possibly wouldn't haved notice this "bug" (or it would have happened randomly), as the garabge collector would probably have garabge collected the stream returned by the File.Create() method call before you attempted to open up the file again.

With respect to why your new code snippet works. It is working because of the behaviour of the StreamWriter constructor you have used. If you look in the MSDN documentation for this constructor (see http://msdn2.microsoft.com/en-us/library/36b035cb.aspx) you will find the following description of it's behaviour:

Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the default encoding and buffer size. If the file exists, it can be either overwritten or appended to. If the file does not exist, this constructor creates a new file.

and then a little further down when it describes the purpose of the constructor's arguments:

append

Determines whether data is to be appended to the file. If the file exists and append is false, the file is overwritten. If the file exists and append is true, the data is appended to the file. Otherwise, a new file is created.

See the bits that I have highlighted in bold.

Hope this helps,

Christopher Fairbairn






Re: Smart Devices VB and C# Projects Adding a text file in the mc1000 scanner

quest3

Thanks very much now i got it how exactly it is working for some time i was wondered how the hell it creating file as i dint specified to create file anywhere.