Click here to Skip to main content
15,896,606 members
Home / Discussions / C#
   

C#

 
GeneralRe: Object Serialization with an ArrayList Pin
cyn86-Aug-07 20:17
cyn86-Aug-07 20:17 
GeneralRe: Object Serialization with an ArrayList Pin
Hessam Jalali6-Aug-07 21:25
Hessam Jalali6-Aug-07 21:25 
GeneralRe: Object Serialization with an ArrayList Pin
Spyder_Snyper7-Aug-07 4:39
Spyder_Snyper7-Aug-07 4:39 
GeneralRe: Object Serialization with an ArrayList Pin
Hessam Jalali7-Aug-07 6:43
Hessam Jalali7-Aug-07 6:43 
QuestionCannot generate SSPI context error Pin
xbiplav6-Aug-07 5:17
xbiplav6-Aug-07 5:17 
AnswerRe: Cannot generate SSPI context error Pin
snorkie6-Aug-07 11:49
professionalsnorkie6-Aug-07 11:49 
AnswerRe: Cannot generate SSPI context error Pin
mav.northwind7-Aug-07 1:18
mav.northwind7-Aug-07 1:18 
QuestionFile Encoding Pin
Sean_B6-Aug-07 4:22
Sean_B6-Aug-07 4:22 
Hello,

I'm hoping someone can help me determine how a file is encoded when I read it in c#.

I'm reading in thousands of files that stretch over a couple of decades, and the files seem to keep changing their encryption.

the files are comma delimited and they have a header record.

I'm using the column names in the header record to create a column mapping for SqlBulkCopy, this is because over the years column names have changed or new columns have been added.

My program stops if an unrecognised column name is found and then I can check it and enter it into a mapping table.

However I'm occasionally getting strange characters in the column headings. I initially got around this problem by determing the encoding (using the peice of code at the end of the message) however I still seem to be coming across files where I can't tell what the encoding is and I'm getting strange characters (mainly when a "£" sign is not recognised).

I'm reading the file using this

using (StreamReader sr = new StreamReader(sourceFile, Encoding(sourceFile), true)


Can anyone help with this, I would be happy to replace unrecgognised characters if possible.


--------determine the type of encoding----
public static System.Text.Encoding Encoding(string sourceFile)
{
System.IO.FileStream file = null;
System.Text.Encoding enc = System.Text.Encoding.ASCII;

try
{
file = new System.IO.FileStream(sourceFile,
FileMode.Open, FileAccess.Read, FileShare.Read);

if (file.CanSeek)
{
byte[] bom = new byte[4]; // Get the byte-order mark, if there is one
file.Read(bom, 0, 4);
if ((bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) || // utf-8
(bom[0] == 0xff && bom[1] == 0xfe) || // ucs-2le, ucs-4le, and ucs-16le
(bom[0] == 0xfe && bom[1] == 0xff) || // utf-16 and ucs-2
(bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff)) // ucs-4
{
enc = System.Text.Encoding.Unicode;
}
else
{
enc = System.Text.Encoding.ASCII;
}
// Now reposition the file cursor back to the start of the file
file.Seek(0, System.IO.SeekOrigin.Begin);
}
else
{
enc = System.Text.Encoding.ASCII;
}

// Close the file: never forget this step!
file.Close();
return enc;
}
--------determine the type of encoding end----






Sean

AnswerRe: File Encoding Pin
Luc Pattyn6-Aug-07 4:37
sitebuilderLuc Pattyn6-Aug-07 4:37 
GeneralRe: File Encoding Pin
Sean_B6-Aug-07 5:26
Sean_B6-Aug-07 5:26 
QuestionPNG compression? Pin
Mad_Mike6-Aug-07 3:52
Mad_Mike6-Aug-07 3:52 
AnswerRe: PNG compression? [modified] Pin
PhilDanger6-Aug-07 4:11
PhilDanger6-Aug-07 4:11 
GeneralRe: PNG compression? Pin
Mad_Mike6-Aug-07 4:14
Mad_Mike6-Aug-07 4:14 
GeneralRe: PNG compression? Pin
PhilDanger6-Aug-07 4:29
PhilDanger6-Aug-07 4:29 
GeneralRe: PNG compression? Pin
Mad_Mike6-Aug-07 5:04
Mad_Mike6-Aug-07 5:04 
Questionexcel Pin
TAREQ F ABUZUHRI6-Aug-07 3:10
TAREQ F ABUZUHRI6-Aug-07 3:10 
AnswerRe: excel Pin
Nisar Inamdar6-Aug-07 3:13
Nisar Inamdar6-Aug-07 3:13 
GeneralRe: excel Pin
TAREQ F ABUZUHRI6-Aug-07 4:03
TAREQ F ABUZUHRI6-Aug-07 4:03 
AnswerRe: excel Pin
Vasudevan Deepak Kumar6-Aug-07 3:23
Vasudevan Deepak Kumar6-Aug-07 3:23 
AnswerRe: excel Pin
Giorgi Dalakishvili6-Aug-07 3:25
mentorGiorgi Dalakishvili6-Aug-07 3:25 
AnswerRe: excel Pin
Bhavesh Bagadiya6-Aug-07 4:26
Bhavesh Bagadiya6-Aug-07 4:26 
GeneralRe: excel Pin
\laddie6-Aug-07 4:29
\laddie6-Aug-07 4:29 
AnswerRe: excel Pin
\laddie6-Aug-07 4:27
\laddie6-Aug-07 4:27 
QuestionAdd variable to object reference Pin
Rick van Woudenberg6-Aug-07 2:30
Rick van Woudenberg6-Aug-07 2:30 
AnswerRe: Add variable to object reference Pin
Luc Pattyn6-Aug-07 2:52
sitebuilderLuc Pattyn6-Aug-07 2:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.