Windows Develop Bookmark and Share   
 index > Windows Forms General > Windows Service
 

Windows Service

Hi all!

I've built Windows Service which is watching a specific folder and reads the csv-files which are dropped to the folder. From csv-files there's coming example dates. Now the problemis that I can't get dates to be recognize like I would like to. The error is: "System.InvalidCastException: Conversion from string "31.05.2005" to type 'Date' is not valid." The same date works in my development machine and also in my testing machine, but when the service is installed to the production machine dates are not recognized anymore. I've set globalization in the app.config and also setting dateformat in the code, but nothing seems to work. Does anybody have an idea what I could be missing here?

Thanks.
sean.net  Monday, June 15, 2009 6:02 AM
Hi Sean,

The FileSystemWatcher runs a thread to detect the changes in the specified directory, we should change the culture for that thread to Russian before the converting, i.e.

 Protected Overrides Sub OnStart(ByVal args() As String)
        Me.FileSystemWatcher1.Path = "c:\test"
        AddHandler Me.FileSystemWatcher1.Created, AddressOf fsw_changed
    End Sub

    Private Sub fsw_changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created

        Dim newCulture As CultureInfo = New CultureInfo("ru-RU")
        newCulture.DateTimeFormat.ShortDatePattern = "dd.MM.yyyy"
        newCulture.DateTimeFormat.DateSeparator = "."
        Thread.CurrentThread.CurrentCulture = newCulture

        Try
            Using sw As New StreamWriter("c:\TEMP\yzxservice.txt")
                Dim rightDate As Date
                rightDate = "31.05.2005"
                sw.WriteLine("Something happens at " & rightDate.ToString())
            End Using
        Catch ex As Exception
            Using sw As New StreamWriter("c:\TEMP\yzxservice.txt")
                sw.WriteLine(ex.Message)
            End Using
        End Try
    End Sub



Best Regards,
Zhi-Xin
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
  • Marked As Answer bysean.net Wednesday, June 24, 2009 10:31 AM
  •  
Zhi-Xin Ye  Wednesday, June 24, 2009 10:19 AM
Hi sean,

How do you convert the string to date? You can try something like this to convert the string:

string s = "31.05.2005";
DateTime dt = DateTime.ParseExact(s, "dd.mm.yyyy", CultureInfo.InvariantCulture);

Please try the suggestion and let me know whether it helps.

Best Regards,
Zhi-Xin





Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
Zhi-Xin Ye  Monday, June 15, 2009 8:10 AM
Thank you! That did the trick.

Still I would like to hearif somebody has any idea what could cause the used dates were not recognized even if regional settings were set right in the server and in the application.
sean.net  Thursday, June 18, 2009 5:48 AM
How do you convert the string to date?
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
Zhi-Xin Ye  Thursday, June 18, 2009 5:51 AM

I get the dates from a csv-file like 20050531 so I have to make them right format like this:

Dim rightDate As Date
rightDate = line(8).ToString.Substring(6, 2) & "." & line(8).ToString.Substring(4, 2) & "." & line(8).ToString.Substring(0, 4)

line is array where I have splitted row what I'm getting from csv.

sean.net  Thursday, June 18, 2009 6:01 AM

Hi sean,


The "dd.MM.yyyy" format needs Russian locale, the Location setting on your production machine is not Russia, so the converting from the "dd.MM.yyyy" formatted string to Date type is invalid. You can change the culture for the current thread to valid the converting, e.g.

Dim rightDate As Date
Threading.Thread.CurrentThread.CurrentCulture = New Globalization.CultureInfo("ru-RU")
rightDate = "31.05.2005"

However, it's better to use the DateTime.Parse method or DateTime.ParseExact method to convert from string to Date type. For example:

string s = "31.05.2005";
DateTime dt = DateTime.ParseExact(s, "dd.mm.yyyy", CultureInfo.InvariantCulture);

Best Regards,
Zhi-Xin


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
Zhi-Xin Ye  Thursday, June 18, 2009 7:56 AM
Thanks. I think that I now try to remember to use alwaysDateTime.Parse or DateTime.ParseExact.

But in this applicationI've set CurrentCulture like you wrote for whole service in OnStart of application like this:

Dim newCulture As CultureInfo = New CultureInfo("ru-RU")
newCulture.DateTimeFormat.ShortDatePattern = "dd.MM.yyyy"
newCulture.DateTimeFormat.DateSeparator = "."
Thread.CurrentThread.CurrentCulture = newCulture

Also I've set globalization tag in app.config. I thought that those settings would have been enough but it seems not.
sean.net  Thursday, June 18, 2009 8:51 AM
Hi Sean,

Does the convertion occur on a different thread ? If you specify the Russian culture for the thread which doing the convertion, then the convertion should succeed. I test with the following code, it works fine.

Protected Overrides Sub OnStart(ByVal args() As String)

        Try

            Dim newCulture As CultureInfo = New CultureInfo("ru-RU")
            newCulture.DateTimeFormat.ShortDatePattern = "dd.MM.yyyy"
            newCulture.DateTimeFormat.DateSeparator = "."
            Thread.CurrentThread.CurrentCulture = newCulture

            Dim rightDate As Date
            rightDate = "31.05.2005"

            Using sw As New StreamWriter("c:\TEMP\yzxservice.txt")
                sw.WriteLine("Service starts at " & rightDate.ToString())
            End Using

        Catch ex As Exception
            Using sw As New StreamWriter("c:\TEMP\yzxservice.txt")
                sw.WriteLine(ex.Message)
            End Using
        End Try

    End Sub


Best Regards,
Zhi-Xin


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
Zhi-Xin Ye  Friday, June 19, 2009 10:10 AM

In OnStart I'veFileSystemWatcher which triggers methodwhen afile is created (AddHandler fsw.Created, AddressOf fsw_changed). Then the method is doing the job including conversion ofdates.

sean.net  Wednesday, June 24, 2009 9:47 AM
Hi Sean,

The FileSystemWatcher runs a thread to detect the changes in the specified directory, we should change the culture for that thread to Russian before the converting, i.e.

 Protected Overrides Sub OnStart(ByVal args() As String)
        Me.FileSystemWatcher1.Path = "c:\test"
        AddHandler Me.FileSystemWatcher1.Created, AddressOf fsw_changed
    End Sub

    Private Sub fsw_changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created

        Dim newCulture As CultureInfo = New CultureInfo("ru-RU")
        newCulture.DateTimeFormat.ShortDatePattern = "dd.MM.yyyy"
        newCulture.DateTimeFormat.DateSeparator = "."
        Thread.CurrentThread.CurrentCulture = newCulture

        Try
            Using sw As New StreamWriter("c:\TEMP\yzxservice.txt")
                Dim rightDate As Date
                rightDate = "31.05.2005"
                sw.WriteLine("Something happens at " & rightDate.ToString())
            End Using
        Catch ex As Exception
            Using sw As New StreamWriter("c:\TEMP\yzxservice.txt")
                sw.WriteLine(ex.Message)
            End Using
        End Try
    End Sub



Best Regards,
Zhi-Xin
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
  • Marked As Answer bysean.net Wednesday, June 24, 2009 10:31 AM
  •  
Zhi-Xin Ye  Wednesday, June 24, 2009 10:19 AM
Ok, so that's why changing the culture didn't work before. Thank you for clarification.
sean.net  Wednesday, June 24, 2009 10:30 AM

You can use google to search for other answers

Custom Search

More Threads

• Virtual Keyboard For Touchscreen
• Panel with PictureBox Drag
• First displayed char in a RichTextBox - scroll to line?
• changing the default border style of mdi child forms
• Get all class name control in Namespace System.Windows.Forms support in .Net 2.0
• Any event get fired on controls, while tab selected index changed?
• Encyrption
• SelectedValue remains null after selecting a ListBox item
• Prevent standard text edit context menu display
• Change scroll bar style...?