PrintJob TimeSubmitted如何转换为datetime

时间:2013-06-09 18:06:13

标签: c# printing

我有一个C#类,它接受作业打印机及其一些属性,包括TimeSubmitted,以这种格式存储为DateTime:20130608204517.699000-300

我用这个属性定义了一个对象:

public class PrinterJob
{
    public String Document { get; set; }
    public string JobName { get; set; }
    public UInt32 JobId { get; set; }
    public string JobStatus { get; set; }
    //===========================================================================
    // This property doesn't cast adecuate 
    //===========================================================================
    public Datetime TimeSubmitted { get; set; }
}

当我尝试施放DateTime时(在运行时):

foreach (ManagementObject prntJob in prntJobCollection)
{
    System.String jobName = prntJob.Properties["Name"].Value.ToString();

    //Job name would be of the format [Printer name], [Job ID]
    char[] splitArr = new char[1];
    splitArr[0] = Convert.ToChar(",");
    string prnterName = jobName.Split(splitArr)[0];
    if (String.Compare(prnterName, printerName, true) == 0)
    {
        PrinterJob prtobj = new PrinterJob();
        prtobj.Document = prntJob.Properties["Document"].Value.ToString();
        prtobj.JobName = prntJob.Properties["Name"].Value.ToString();
        var id = prntJob.Properties["JobId"].Value;
        prtobj.JobId = (UInt32)prntJob.Properties["JobId"].Value;
        prtobj.JobStatus = prntJob.Properties["JobStatus"].Value.ToString();
        //============================================================================  
        // Here The cast doesn't work. It comes as: 20130608204517.699000-300 
        // It throws an InvalidCastException
        //============================================================================  
        prtobj.TimeSubmitted = (DateTime)prntJob.Properties["TimeSubmitted"].Value;
        jobList.Add(prtobj);
    }
} 

将此值转换为DateTime的正确方法是什么?

2 个答案:

答案 0 :(得分:2)

添加对System.Management

的引用
var datetime = ManagementDateTimeConverter.ToDateTime("20130608204517.699000-300");

答案 1 :(得分:0)

不是简单的日期时间吗? (in.yyyyMMddHHmmss) 我不知道点后面的部分是什么意思。