Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
devwiki:cs [2022/04/05 10:46] – [Mac and Visual Studio with C#] ying | devwiki:cs [2022/05/07 06:53] (current) – [C# Basic] ying | ||
---|---|---|---|
Line 39: | Line 39: | ||
List< | List< | ||
numberList.Add(32); | numberList.Add(32); | ||
+ | // write out list as string | ||
+ | Console.WriteLine( string.Join(", | ||
+ | |||
int[] extraArray = {10,11,12}; | int[] extraArray = {10,11,12}; | ||
Line 62: | Line 65: | ||
// c# has Stack object for Push() and Pop() | // c# has Stack object for Push() and Pop() | ||
// c# extra: SortedSet, HashSet | // c# extra: SortedSet, HashSet | ||
+ | </ | ||
+ | |||
+ | * timestamp to datetime conversion <code csharp> | ||
+ | using System; | ||
+ | public DateTime UnixTimeStampToDateTime(double unixTimeStamp) | ||
+ | { | ||
+ | // Unix timestamp is seconds past epoch | ||
+ | DateTime dateTime = new DateTime(1970, | ||
+ | dateTime = dateTime.AddSeconds(unixTimeStamp).ToLocalTime(); | ||
+ | return dateTime; | ||
+ | } | ||
</ | </ | ||
====== C# and Visual Studio Community edition ====== | ====== C# and Visual Studio Community edition ====== | ||
Line 115: | Line 129: | ||
* write your cs with gtk lib, save as cs file | * write your cs with gtk lib, save as cs file | ||
* compile with < | * compile with < | ||
- | * you get a exe | + | * you get a exe < |
- | * ref: https:// | + | * ref: |
+ | * https:// | ||
+ | * https:// | ||
- | ====== method: launch script | + | ===== method: launch script ===== |
* AppName file with chmod +x to make it launch script, the script will use its name(AppName this case) to launch AppName.exe next to it<code bash AppName> | * AppName file with chmod +x to make it launch script, the script will use its name(AppName this case) to launch AppName.exe next to it<code bash AppName> | ||
Line 132: | Line 148: | ||
</ | </ | ||
+ | ===== method: mac app auto build script ===== | ||
+ | * AppName_build file with chmod +x to make it build script, the script will use its name and _build (AppName_build this case) to pack AppName.exe next to it into a AppName.app mac app<code bash AppName_build> | ||
+ | #!/bin/sh | ||
+ | # auto build Mono GTK# App on Mac by ying | ||
+ | # v0.1 (2022.04.04) | ||
+ | app_build_name=`basename $0` | ||
+ | part_list=(${app_build_name// | ||
+ | app_name=${part_list[0]} | ||
+ | echo $app_name | ||
+ | macpack -m:cocoa -o:./App -n $app_name -a: | ||
+ | </ | ||
====== .NET framework install requirement for your C# app ====== | ====== .NET framework install requirement for your C# app ====== | ||
Line 142: | Line 168: | ||
* like, win7 = v3.5 .NET; win8 = v4.5 .NET; win10 = v4.6 .NET; xp = v1.0 .NET | * like, win7 = v3.5 .NET; win8 = v4.5 .NET; win10 = v4.6 .NET; xp = v1.0 .NET | ||
+ | | Target framework | version | C# version default | | ||
+ | | .NET | 6.x | C# 10 | | ||
+ | | .NET | 5.x | C# 9.0 | | ||
+ | | .NET Core | 3.x | C# 8.0 | | ||
+ | | .NET Core | 2.x | C# 7.3 | | ||
+ | | .NET Standard | ||
+ | | .NET Standard | ||
+ | | .NET Standard | ||
+ | | .NET Framework | all | C# 7.3 | | ||
+ | |||
+ | * if you use Gtk# ui, you also need to install gtk# (so basically .NET + GTK# make all those exe run fine) | ||
+ | * (GTK# for .NET: Installer for running Gtk#-based applications on Microsoft .NET:) | ||
+ | * https:// | ||
+ | |||
+ | * note: no need the mono installer, since if use mono, you need to use mono AppName.exe to run it, with .NET installed, you can direct click exe and run | ||
+ | |||
+ | * Problem and Solution: | ||
+ | * BadImageFormatException error | ||
+ | * Solution: make sure install x86 version of .NET, since most app made are 32bit, not 64bit. | ||
+ | |||
+ | Problem and Solution: | ||
+ | * VS 2019 code complain C# version 7.3 cant support certain thing, use C# ver 9. But your right click on your Project Name > Property : (right side) Build tab - Advanced button > Language version (it says " | ||
+ | * Solution: go your project folder, edit .csproj file, add following code < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | * ref: | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
====== .Net UI frameworks options ====== | ====== .Net UI frameworks options ====== | ||
Line 250: | Line 308: | ||
</ | </ | ||
+ | |||
+ | ====== My App - Output Selected Item in Top Most Explorer in CSharp ====== | ||
+ | |||
+ | <code csharp Program.cs> | ||
+ | using System; | ||
+ | using System.Collections.Generic; | ||
+ | using System.Linq; | ||
+ | using System.Text; | ||
+ | |||
+ | /* | ||
+ | ListSelection: | ||
+ | * v0.1: (2022.05.01) list selected item in top most explorer | ||
+ | |||
+ | use with python: | ||
+ | import subprocess | ||
+ | app = r" | ||
+ | info=subprocess.Popen( ' | ||
+ | out, err = info.communicate() | ||
+ | result_list = [x.strip() for x in out.split(' | ||
+ | |||
+ | */ | ||
+ | namespace ListSelection | ||
+ | { | ||
+ | | ||
+ | class Program | ||
+ | { | ||
+ | // need user32 to use get foreground window | ||
+ | [System.Runtime.InteropServices.DllImport(" | ||
+ | private static extern IntPtr GetForegroundWindow(); | ||
+ | |||
+ | [System.Runtime.InteropServices.DllImport(" | ||
+ | private static extern IntPtr GetTopWindow(IntPtr hWnd); | ||
+ | |||
+ | [System.Runtime.InteropServices.DllImport(" | ||
+ | private static extern bool IsWindowVisible(IntPtr hWnd); | ||
+ | |||
+ | [System.Runtime.InteropServices.DllImport(" | ||
+ | static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd); | ||
+ | |||
+ | enum GetWindow_Cmd : uint | ||
+ | { | ||
+ | GW_HWNDFIRST = 0, | ||
+ | GW_HWNDLAST = 1, | ||
+ | GW_HWNDNEXT = 2, | ||
+ | GW_HWNDPREV = 3, | ||
+ | GW_OWNER = 4, | ||
+ | GW_CHILD = 5, | ||
+ | GW_ENABLEDPOPUP = 6 | ||
+ | } | ||
+ | |||
+ | // stathread needed to fix unable to cast com object error, as it needs sta thread to use shell object | ||
+ | // https:// | ||
+ | [STAThread] | ||
+ | static void Main(string[] args) | ||
+ | { | ||
+ | List< | ||
+ | /*IntPtr fg_handle = GetForegroundWindow(); | ||
+ | IntPtr top_handle = GetTopWindow((IntPtr)null); | ||
+ | if (top_handle != IntPtr.Zero) | ||
+ | { | ||
+ | // | ||
+ | if (IsWindowVisible(top_handle)) | ||
+ | { | ||
+ | order_list.Add(top_handle); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | IntPtr top_next = GetWindow(top_handle, | ||
+ | while (top_next != IntPtr.Zero) { | ||
+ | // | ||
+ | if (IsWindowVisible(top_next)) { | ||
+ | order_list.Add(top_next); | ||
+ | } | ||
+ | top_next = GetWindow(top_next, | ||
+ | } | ||
+ | /* | ||
+ | Console.WriteLine(" | ||
+ | Console.WriteLine(" | ||
+ | for (int i = 0; i < order_list.Count; | ||
+ | { | ||
+ | Console.WriteLine((int)order_list[i]); | ||
+ | } | ||
+ | */ | ||
+ | | ||
+ | |||
+ | Shell32.Shell shell = new Shell32.Shell(); | ||
+ | // fix reference: right click project, Add > Reference; in COM, check on Microsoft Shell Controls and Automation | ||
+ | List< | ||
+ | foreach (SHDocVw.InternetExplorer window in shell.Windows()) | ||
+ | { | ||
+ | win_list.Add(window.HWND); | ||
+ | } | ||
+ | /* | ||
+ | Console.WriteLine(" | ||
+ | Console.WriteLine(" | ||
+ | for (int i = 0; i < win_list.Count; | ||
+ | { | ||
+ | Console.WriteLine(win_list[i]); | ||
+ | } | ||
+ | */ | ||
+ | |||
+ | // top most explorer | ||
+ | int top_explorer = 0; | ||
+ | for (int i = 0; i < order_list.Count; | ||
+ | { | ||
+ | if (win_list.Contains((int)order_list[i])) { | ||
+ | top_explorer = (int)order_list[i]; | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | List< | ||
+ | foreach (SHDocVw.InternetExplorer window in shell.Windows()) | ||
+ | { | ||
+ | // fix SHDocVw: right click project, Add > Reference; in COM, check on Microsoft Internet Controls | ||
+ | if (window.HWND == top_explorer) | ||
+ | { | ||
+ | Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems(); | ||
+ | foreach (Shell32.FolderItem item in items) | ||
+ | { | ||
+ | selected.Add(item.Path); | ||
+ | Console.WriteLine(item.Path); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | Console.WriteLine("# | ||
+ | //return (string.Join(", | ||
+ | |||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||