2019年4月25日 星期四

C# 執行vbs語法

Thread executionThread = new Thread(new ParameterizedThreadStart(RunPrograms));
                    ProcessStartInfo startInfo = new ProcessStartInfo(ProgramPath);
                    startInfo.Arguments = ProgramArguments;
                    startInfo.UseShellExecute = false;
                    startInfo.RedirectStandardInput = false;
                    ExecutionInstance executionInstance = new ExecutionInstance(startInfo, FilePath);
                    executionThread.Start(executionInstance);
                    Log.WriteToLog("Info", "Added program to execute \"{0}\" argument \"{1}\".", ProgramPath, ProgramArguments);

public static void RunPrograms(object source)
        {
            try
            {
                bool success = false;
                int retries = 100;

                ExecutionInstance executionInstance = (ExecutionInstance)source;
                FileStream fileStream = null;

                while (!success)
                {
                    try
                    {
                        fileStream = File.Open(executionInstance.FilePath, FileMode.Open, FileAccess.Read, FileShare.None);
                        success = true;
                    }
                    catch (Exception ex)
                    {
                        Log.WriteToLog("Error", "Unhandled exception occurred while waiting for \"{0}\" to become available. Type: {1} Message: {2}", executionInstance.FilePath, ex.GetType().FullName, ex.Message);
                        Thread.Sleep(1000);
                        if (--retries == 0)
                        {
                            Log.WriteToLog("Error", "Exception occurred while opening the file. File: {0} Message: {1}", executionInstance.FilePath, ex.Message);
                            break;
                        }
                    }
                }

                fileStream.Close();

                try
                {
                    Log.WriteToLog("Info", "Running program in response to event for {2}: {0}{1}.", executionInstance.StartInfo.FileName, (executionInstance.StartInfo.Arguments != "" ? " " + executionInstance.StartInfo.Arguments : ""), executionInstance.FilePath);
                    Process executionProcess = Process.Start(executionInstance.StartInfo);
                    executionProcess.WaitForExit();
                    executionProcess.Close();

                    string path = Path.GetDirectoryName(executionInstance.FilePath);
                    string[] filePaths = Directory.GetFiles(path);

                    string file = Path.GetFileName(executionInstance.FilePath);

                }
                catch (Exception ex)
                {
                    Log.WriteToLog("Error", "Exception occurred while running {0}. Type: {1} Message: {2}", (executionInstance.StartInfo != null ? "\"" + executionInstance.StartInfo.FileName + "\"" : ""), ex.GetType().FullName, ex.Message);
                }
            }
            catch (Exception ex)
            {
                Log.WriteToLog("Error", "Exception occurred. Type: {0} Message: {1}", ex.GetType().FullName, ex.Message);
            }
        }

用C# 執行command語法

                    System.Diagnostics.Process p = new System.Diagnostics.Process();
                    p.StartInfo.FileName = ProgramPath;
                    p.StartInfo.WorkingDirectory = @"D:\";
                    p.StartInfo.Arguments = ProgramArguments + FilePath;
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardInput = true;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.RedirectStandardError = true;
                    p.StartInfo.CreateNoWindow = true;

                    //Log.WriteToLog("Info", "filename=" + p.StartInfo.FileName + ", Arguments=" + p.StartInfo.Arguments);
                    Log.WriteToLog("Info", "Added program to execute \"{0}\" argument \"{1}\".", p.StartInfo.FileName, p.StartInfo.Arguments);
                    p.Start();

                    using (StreamReader myStreamReader = p.StandardOutput)
                    {
                        string result = myStreamReader.ReadToEnd();
                        p.WaitForExit();
                        p.Close();
                        Log.WriteToLog("Info", "relation import finished!");
                    }

2018年11月14日 星期三

xml 轉 class 並做條件篩選

//class 宣告時用list不要用array

//篩選時用removeall

           
// 透過XmlDocument讀入
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(service_result.Replace("<n:", "<").Replace("</n:", "</").Replace(" n:", " "));
            // 讀取HEADER節點
            XmlNode responseNode = xmlDoc.LastChild;
            // 透過Json.NET將XmlNode轉為Json格式字串
            string jsonText = JsonConvert.SerializeXmlNode(responseNode);

            jsonText = jsonText.Replace("@", "");
            var obj = JsonConvert.DeserializeObject(fortifyService.PathManipulation(jsonText));
            var f = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);

            // 透過Json.NET反序列化為物件
            var responseObj = JsonConvert.DeserializeObject<Response>(fortifyService.PathManipulation(f));

            for (int i = 0; i < responseObj.GETSEARCHRESULTS.OUTPUT.MATCHES.MATCH.Count(); i++)
            {
                if (i == 0)
                    responseObj.GETSEARCHRESULTS.OUTPUT.MATCHES.MATCH[i].SCORE = 70;

                if (i == 1)
                    responseObj.GETSEARCHRESULTS.OUTPUT.MATCHES.MATCH[i].SCORE = 90;
            }

            responseObj.GETSEARCHRESULTS.OUTPUT.MATCHES.MATCH.RemoveAll(x => x.SCORE < iScore);

       
        public class Response
        {
        public Header GETSEARCHRESULTS { get; set; }
        }

        public class Header
        {
            public Ouput OUTPUT { get; set; }
        }

        public class Ouput
        {
            public string TRANSACTIONID { get; set; }
            public Matches MATCHES { get; set; }
            public Searchstate SEARCHSTATE { get; set; }
            public Approvestate APPROVESTATE { get; set; }
            public string ANNOTATION { get; set; }
            public string REQUESTTIME { get; set; }
            public string CIFRULE { get; set; }
        }

        public class Matches
        {
            public string COUNT { get; set; }
            public List<Match> MATCH { get; set; }
        }

        public class Match
        {
            public string N { get; set; }
            public string MATCHNAME { get; set; }
            public string ORIGINALSDNNAME { get; set; }
            public string MATCHTEXT { get; set; }
            public string MATCHPROGRAM { get; set; }
            public string MATCHTYPE { get; set; }
            public string MATCHREMARKS { get; set; }
            public int SCORE { get; set; }
        }

        public class Searchstate
        {
            public string STATUS { get; set; }
            public string DATETIME { get; set; }
        }

        public class Approvestate
        {
            public string APPROVED { get; set; }
            public string DATETIME { get; set; }
        }

2018年8月7日 星期二

[CMD] 批次語法檢查檔案大小

set file="test.cmd"
set maxbytesize=1000
FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA
if %size% LSS %maxbytesize% (
    echo.File is ^< %maxbytesize% bytes
) ELSE (
    echo.File is ^>= %maxbytesize% bytes
)


delfile.bat:
FOR %%F IN ("C:\abc\a*b") DO (IF %%~zF LSS 1 del %%F)

如果要用另一個bat去呼叫,前面不用加 call
call C:\abc\delsize0activity.bat > D:\abc\delsize0.log

EQU – 等於
NEQ – 不等於
LSS – 小於
LEQ – 小於或等於
GTR – 大於
GEQ – 大於或等於


參考:http://fly-dolphin.blogspot.com/2012/02/blog-post.html
http://white5168.blogspot.com/2012/09/batch-if.html#.W2lDiNIzbIU
http://kukuso1983.blogspot.com/2010/09/0-setp1.html

2018年8月2日 星期四

[note] assembly 手動安裝修改步驟

C:\Windows\assembly modify process:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion\DisableCacheViewer set 0



2018年7月13日 星期五

2018年6月29日 星期五

C# 檢查 file是否被開啟

                                FileStream fs = null;
                                bool inUse = true;
                                try
                                {
                                    fs = new FileStream(fortifyService.PathManipulation(e.FullPath), FileMode.Open, FileAccess.Read, FileShare.None);
                                    inUse = false;
                                }
                                catch
                                {

                                }
                                finally
                                {
                                    if (fs != null)
                                        fs.Close();
                                    Log.WriteToLog("Info", "file:{0}, inuse={1};", fortifyService.PathManipulation(e.FullPath), inUse.ToString());
                                }