顯示具有 外部啟動 標籤的文章。 顯示所有文章
顯示具有 外部啟動 標籤的文章。 顯示所有文章

2022年7月25日 星期一

[C#]啟動外部程式且切換工作目錄

 public static bool OpenPress(string FileName, string Arguments)//開啟外部檔案

{
    Process pro = new Process();
    if (System.IO.File.Exists(FileName))
    {
        //設定工作目錄為當前工作目錄
        string sPath = string.Empty;
        sPath = Path.GetDirectoryName(FileName).ToString();
        System.IO.Directory.SetCurrentDirectory(sPath);

        pro.StartInfo.FileName = FileName;
        pro.StartInfo.Arguments = Arguments;
        pro.StartInfo.CreateNoWindow = true;
        pro.StartInfo.UseShellExecute = false;
        pro.StartInfo.RedirectStandardInput = true;
        pro.StartInfo.RedirectStandardOutput = true;
        pro.StartInfo.CreateNoWindow = true;
        pro.Start();
        //切換為原工作目錄
        System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
        return true;
    }
    return false;
}

2022年5月9日 星期一

[C#]程式焦點切換後恢復焦點的方式 foucs

 使用user32.dll 的Api,應用情境如以下:
在程式A上點擊按鈕後啟動B程式在背景執行,此時的焦點需要回到A程式
正常流程焦點會在B,透過以下方式可以將焦點設定為A


範例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Test
{
    public partial class PlayVoice : Form
    {
        public PlayVoice()
        {
            InitializeComponent();
        }


        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetForegroundWindow", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetF();             //獲得本窗體的控制代碼
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetF(IntPtr hWnd);    //設定此窗體為活動窗體

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Handle != GetF())           //如果本視窗沒有獲得焦點
                SetF(this.Handle);                //設定本視窗獲得焦點
        }
    }
}

2019年1月13日 星期日

[C#]外部程式 縮小 最大化 隱藏

需要 using System.Diagnostics;

string FileName = "123.exe"


----------------------程式碼-------------------------------------------

 Process pro = new Process();
 pro.StartInfo.FileName = FileName;
 pro.StartInfo.Arguments = Arguments;
 pro.StartInfo.CreateNoWindow = true;

pro.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;//縮小(則一使用)
pro.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;//最大化(則一使用)
pro.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//隱藏(則一使用)
 pro.Start();

2019年1月11日 星期五

[C#]開啟外部執行檔/關閉外部執行檔

兩個函式宣告,需要 using System.Diagnostics;

----------------------使用方式-------------------------------------------
OpenPress("AAA.exe"" ");//開啟外部檔案
ClosePress("AAA.exe");//關閉外部檔案
----------------------以下為函式-------------------------------------------

public static bool OpenPress(string FileName, string Arguments)//開啟外部檔案
       {
           Process pro = new Process();
           if(System.IO.File.Exists(FileName))
           {
               pro.StartInfo.FileName = FileName;
               pro.StartInfo.Arguments = Arguments;
               pro.StartInfo.CreateNoWindow = true;
               pro.StartInfo.CreateNoWindow = true;
               pro.StartInfo.UseShellExecute = false;
               pro.StartInfo.RedirectStandardInput = true;
               pro.StartInfo.RedirectStandardOutput = true;
               pro.StartInfo.CreateNoWindow = true;               
               pro.Start();
               return true;
           }
           return false;
       }
public static bool ClosePress(string FileName)//關閉外部檔案
       {
           Process[] proa = Process.GetProcesses();//獲取任務管理器中所有進程
           foreach(Process p1 in proa)
           {
               try
               {
                   string processName = p1.ProcessName.ToLower().Trim();
                   if(processName == FileName)
                   {
                       p1.WaitForExit(1000);                       
                       p1.Kill();
                       p1.CloseMainWindow();
                   }
               }
               catch { return false; }
           }
           return true;
       }
----------分隔線 說明--------------------

1.前言
透過程式控制,可在背景執行cmd.exe的命令。

2.說明
程式碼,提供兩種方式執行命令:

//程式碼最少,但是會跳出DOS視窗,閃一下後自行關閉
System.Diagnostics.Process.Start("cmd.exe", @"/c dir");

//在背景執行,無DOS視窗閃爍問題
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"/c dir";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();

[SQL]顯示千分位與小數顯示

  CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) CONVERT style參數說明 1  (expression為 money 或 smallmoney型別): 0 : 預設,保留小數位後兩位,並四捨...