如何在窗口服务中添加MSMQ应用程序C#

时间:2013-09-26 07:12:05

标签: c# .net windows service msmq

我有一个窗口服务应用程序,它在D:中监视文件夹,每当excel文件被删除到文件夹中,然后它转换为xml和txtfile并将其放在另一个文件夹中。 为此,我需要添加MSMQ。如果我在文件夹中删除100个excel文件,那么所有文件都应该通过MSMQ。如何执行此操作。

请找到以下代码,

protected override void OnStart(string[] args)
    {
        fileListenTimer.Start();
        fileListenTimer.Interval = Convert.ToDouble(ConfigurationSettings.AppSettings["FileListenerFrequency"]);
        fileListenTimer.Elapsed += fileListenTimer_Elapsed;
    }

    void fileListenTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        // stop the timer
        fileListenTimer.Stop();

        // Process the file in the directory
        ProcessFile();

        // Start the timer
        fileListenTimer.Start();
    }

    /// <summary>
    /// Process the xlsx files dropped in the folder
    /// </summary>
    public void ProcessFile()
    {
        DirectoryInfo dirInfo = new DirectoryInfo(fileListenerPath);
        List<FileInfo> fileCollection = dirInfo.GetFiles().ToList();
        string[] pathSeparator = { "." };
        foreach (FileInfo fileInfo in fileCollection)
        {
            try
            {
                if (fileInfo.Extension.ToUpper() == ".XLSX")
                {
                    string fileName = fileInfo.Name.Replace("." + fileInfo.Name.Split(pathSeparator, StringSplitOptions.None)[fileInfo.Name.Split(pathSeparator, StringSplitOptions.None).Length - 1], string.Empty);
                    string filePath = fileInfo.FullName;
                    string fullFileName = fileInfo.Name;
                    if (!fileInUse.Keys.Contains(fileInfo.Name))
                    {
                        bool fileReaded = false;
                        bool fileConverted = false;
                        string errorDescription = string.Empty;
                        fileInUse.Add(fullFileName, true);
                        fileReaded = ManipulateFile.ReadExcelFileDetailsAndWriteIntoXML(fileInfo.FullName, fileName, fileListenerPath, connectionString.Replace("[FilePath]", "\"" + fileInfo.FullName + "\""), out errorDescription);
                        if (fileReaded)
                        {
                            fileConverted = ManipulateFile.TransformIntoTextFile(fileInfo.FullName, fileName, fileListenerPath, textFilePublisPath, enableFileArchive, fileArchivePath, out errorDescription);
                            if (fileConverted && fileInUse.Keys.Contains(fullFileName))
                                fileInUse.Remove(fullFileName);
                            else
                            {
                                ManipulateFile.LogError(errorArchivePath, fullFileName, fileName, errorDescription, fileInfo.FullName);
                                if (fileInUse.Keys.Contains(fullFileName))
                                    fileInUse.Remove(fullFileName);

                                if (File.Exists(filePath))
                                    File.Delete(filePath);
                            }
                        }
                        else
                        {
                            ManipulateFile.LogError(errorArchivePath, fullFileName, fileName, errorDescription, fileInfo.FullName);
                            if (fileInUse.Keys.Contains(fullFileName))
                                fileInUse.Remove(fullFileName);
                            if (File.Exists(filePath))
                                File.Delete(filePath);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
    }

    protected override void OnStop()
    {
        // stop the timer
        fileListenTimer.Stop();
    }
}

先谢谢

0 个答案:

没有答案