|
Just put a try/catch block around everything that could fail, and there you have it.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
|
I have done plenty of googling and have gotten close to a solution but wanted to make sure I'm going about it the right way.
I have a data file which is a non delimited fixed field position file that has missing data. I basically want to read each line in and determine if a certain field is blank. If so I use another field in that same record to retrieve the missing data from the database. After getting the missing value I would like to use StreamWriter or StringWriter (not sure which) to write out each line to a new file with the updated field value.
Appreciate any advice you all may have on this subject.
Thanks,
Jason
|
|
|
|
|
I would advice you to do the following:
1. Open the input file
2. Open the output file and create StreamWriter for it
3. Read data line-by-line, and for each line do the check and write to the output file
You need StreamWriter.
The difference:
StreamWriter allows you to write contents to any stream: file, socket, etc;
Stream represents writeable file, socket or other stream (this can be also a memory, or string) - it's not convenient to write strings to stream, that's why you need StreamWriter.
StringWriter writes all contents to a string (in memory);
StringBuilder allows you quickly build string from parts; you needn't it here
|
|
|
|
|
Thanks for the steps and showing the difference between the different classes. Below is what I have so far and I think it will work. My new file contained the 111222333 string for all the missing data which was the desired result.
Thanks,
Jason
StreamReader sr = File.OpenText("C:\\Source.txt");
StreamWriter sw = File.CreateText("C:\\Test1.txt");
while (sr.Peek() != -1)
{
string strRecord = sr.ReadLine();
string strSurname = strRecord.Substring(0, 10);
string strFirstName = strRecord.Substring(11, 8);
string strPupilNum = strRecord.Substring(20, 9).Trim();
string strSSID = strRecord.Substring(373, 9).Trim();
if (strPupilNum == String.Empty)
{
// call db stored procedure to retrieve PupilNum based on SSID
StringBuilder sb = new StringBuilder(strRecord);
sb.Remove(20, 9);
sb.Insert(20, "111222333", 1);
strRecord = sb.ToString();
}
sw.WriteLine(strRecord);
} sw.Close();
|
|
|
|
|
Yeah, that's it.
Don't forget to dispose your StreamWriter and StreamReader .
And next time please wrap your code with <pre lang="C#"> ... </pre> tags
|
|
|
|
|
I am currently working on a simple game for the children in C# WPF. It uses the simple animation from Expression Blend. All that is done is a simple textblock falls in the apps and I wish to make a loop of it such that everytime the loops restarts that is the textblock falls the new contents are displayed to it. XML and LINQ to XML is what I plan to use.
The real problem is the storyboard codes are in XAML and it needs to be looped and added new features from the C# codes. I seem to have no idea how to accomplish that.
I would be grateful if anyone could help. Any favors will be appreciated.Than you in advance.
|
|
|
|
|
anishshrestha wrote: The real problem is the storyboard codes are in XAML and it needs to be looped and added new features from the C# codes
So what's the problem?
Storyboard myStoryBoard = (Storyboard)FindResource("myStoryBoard");
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
hi,
i need to show selected country location through Google map with windows application, so i used "GMap.NET.dll" ,prior it was worked fine, but it is not working , could you please suggest me why it is not working ,and give me suggestion for better way to do this.
|
|
|
|
|
GMap doesn't work in India.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
what do you mean it doesn't work in India?
peace & serenity
|
|
|
|
|
It's called "humor".
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
peace & serenity
|
|
|
|
|
Hi There
I am using Aspose.pdf to generate pdf documents.
The pdf document contains a table which is populated with data from a database.
I have the problem that if the table needs to continue to the next page, the pdf does not generate.
Is there any code that I need to include to allow the document to continue to a new page?
Regards,
|
|
|
|
|
hi,
you can use itextSharp.dll to generate pdfs ,this will get from sourceforge.net , and reference to your project , then write code like this
Document dc = new Document(PageSize.LETTER);
dc.NewPage();
|
|
|
|
|
Thanks, I give it a try
|
|
|
|
|
Just my 2c, I found itextSharp very easy to implement.
|
|
|
|
|
Hello everybody,
I have a file where some application is storing some values. I want to access to this file in read mode but when I try to open the file I have always the message that the process can't acces the file because it is being used by other application.
If I try to open this file with Microsoft excel I have an alert that says that the file is user by other application but I am allowed to open the file in read mode only.
How can I access the file with .net and advoid the exception if it is possible?
Thanks in advance
|
|
|
|
|
cidmi.dovic wrote: I try to open the file I have always the message that the process can't acces the file because it is being used by other application
Can you please post the code used, here ?
Found my answer helpful? Then vote for it. "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder
coolestCoder
|
|
|
|
|
FileInfo file = new FileInfo(ConfigurationSettings.AppSettings["FilePath"]);
using (FileStream fStream = file.OpenRead())
{
}
|
|
|
|
|
Try this:
FileInfo file = new FileInfo(ConfigurationSettings.AppSettings["FilePath"]);
using (FileStream fStream = file.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
{
}
|
|
|
|
|
Yes, it works.
A lot of thanks!!!
|
|
|
|
|
If someone answers your question, vote their answer so the rest of us don't waste time trying to give you the same answer.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Well, I'll try to copy the file and open this new file.
|
|
|
|
|
The other application that's writing to the file may have set the sharing to None, meaning no other app can open the file for any reason as long as the first app has it open.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|