顯示具有 FILE. 標籤的文章。 顯示所有文章
顯示具有 FILE. 標籤的文章。 顯示所有文章

2022年5月19日 星期四

[C#]使用json取得資料/取得多層

 使用環境VS2019
NuGet先加入Newtonsoft.json 
並且使用以下宣告

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

在mamespace中 宣告json要使用的結構類別

    public class cJsonDataItem

    {

        public string server { get; set; }

        public string database { get; set; }

        public string user { get; set; }

        public string password { get; set; }

        public string linkedserver { get; set; }

    }

在取得json 字串(下方用temp代替)後使用以下方式即可取得數值

string temp = "{    "accountingAck": {        "server": "127.0.0.1",        "database": "Accounting_Ack_AZ",        "user": "sa",        "password": "123456789",        "linkedserver": "REMOTE_LINK"    }}";

var dynamic_obj = JObject.Parse(temp);

string s = string.Empty;

StringBuilder strb = new StringBuilder();

StringBuilder strbb = new StringBuilder();

 

foreach (JProperty item in dynamic_obj.Children())

{

    strb.Append(item.Name);

    strbb.Append(item.Value);

}

StringBuilder strbc = new StringBuilder();

StringBuilder strbd = new StringBuilder();

dynamic darray = JsonConvert.DeserializeObject(strbb.ToString());   //此行為動態拿法 不用宣告最上面的類別

cJsonDataItem cjs = JsonConvert.DeserializeObject<cJsonDataItem>(strbb.ToString()); //此行需要使用最上面的類別cJsonDataItem

最終透過cjs 可以輕鬆拿取到各項的字串結果

temp = cjs.server;

temp = cjs.database;

            


2022年3月24日 星期四

[C#]String 檔案路徑分割

 

傳入路徑,後面的參數(upLevel)為層級,透過層級來自動切割要哪一些資料

Ex: 

String  str = "C:\\123\\456\\789\\1.Txt";

str  = GetUpLevelDirectory(str,0);

// str = C:\\123\\456\\789

str = GetUpLevelDirectory(str,1);

//C:\\123\\456

str = GetUpLevelDirectory(str,2);

//C:\\123

-------------------------------------------------------------------------------------------------------------

Path.GetFileName()、Path.GetExtension()、Path.GetFileNameWithoutExtension()

這三個方法是同一群的,依序是取得檔案名稱、取得副檔名、取得不包含副檔名的檔案名稱

--------------------------------------------------------------------------------------------------------------

以下為 function

using System.Diagnostics;

private string GetUpLevelDirectory(string path, int upLevel)
{
    var directory = File.GetAttributes(path).HasFlag(FileAttributes.Directory)
        ? path
        : Path.GetDirectoryName(path);

    upLevel = upLevel < 0 ? 0 : upLevel;

    for (var i = 0; i < upLevel; i++)
    {
        directory = Path.GetDirectoryName(directory);
    }

    return directory;
}

2019年5月20日 星期一

[c++]讀檔迴圈使用注意feof()

 FILE* pFile;
 while (!feof(pFile)) {
  //fread 讀取
  //資料處理
 }
 //如先使用fread讀取,再來判斷feof,當feof判斷為true時就跳離loop
 while (1) {
  //fread讀取
  if (feof(pFile))
    break;
 }

測試環境 vc 2015 mfc

[c++]開檔讀檔寫檔fopen() fwrite()

//在mfc中使用前請先至Stdafx.h 宣告定義
#define _CRT_SECURE_NO_WARNINGS




---------以下為範例--------------------------

FILE *pFile;
char cbuffer[] = { 'h','e','l' ,'l' ,'o' };
pFile = fopen("hello.txt""w");
if (pFile == NULL)
{
 MessageBox(L"empty");
}
else
{
 fwrite(cbuffer,1, sizeof(cbuffer), pFile);
}
fclose(pFile);


測試環境:2015--mfc



參考以下:
FILE * fopen ( const char * filename, const char * mode );
1    

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();

2018年11月29日 星期四

[C++]檢查檔案是否存在

C++ _access和_waccess的使用方法 
概述 
頭文件:<io.h> 判斷文件的訪問權限 
原型 
int _access( 
    const char *path,     int mode ); 
int _waccess(  
    const wchar_t *path,     int mode ); 
參數 
path 
文件或目錄路徑 
mode 
訪問權限設定 
返回值 
如果文件具有指定的訪問權限,則函數返回0;如果文件不存在或者不能訪問指定的權限,則返回-1。 
備註 
當path爲文件時,_access函數判斷文件是否存在,並判斷文件是否可以用
mode值指定的模式進行訪問。當path爲目錄時,_access只判斷指定的目錄是否存
在,在Windows NT和Windows 2000中,所有的目錄都有讀寫權限。 
mode的值和含義如下表所示: mode值 
檢查文件 

00 只檢查文件是否存在 
02 寫權限 
04 讀權限
06 讀寫權限 

_waccess是_access的寬字符版本,_waccess的參數path爲寬字符的字符串,
其他與_access相同。


CString FileName = L"C:\\123.txt";

int buf = 0;
buf = _waccess(FileName , 04);
if (buf != 0)
{
 AfxMessageBox(L"Files error");
 return false;
}

測試環境:MFC

2018年6月5日 星期二

[C#]讀檔,彈出DLG,指定檔名

//彈跳出DLG 選擇TXT檔案 讀取字串

ArrayList alWorkPoint = new ArrayList();
//System.Windows.Forms.ListView listView_CM; 宣告的Listview元件
OpenFileDialog dialog = new OpenFileDialog();
dialog.Title = "Select file";
dialog.InitialDirectory = ".\\";
dialog.Filter = "txt files (*.*)|*.txt";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                StreamReader filer = new StreamReader((dialog.FileName));
                alWorkPoint.Clear();
                listView_CM.Items.Clear();
                string str;
                while ((str = filer.ReadLine()) != null)
                {
                    alWorkPoint.Add(str);
                }
                DecoderFileToListView();
                filer.Close();
            }
//指定檔名,讀取單一檔案
StreamReader filer = new StreamReader(("new.txt"));
string str;
while((str = filer.ReadLine()) != null)
{
    MessageBox.Show(str);
}


測試環境:vs2015 c#.net

[C#]存檔功能、彈出DLG

using System; using System.Collections;

//顯示Dlg 儲存txt檔
ArrayList alWorkPoint = new ArrayList();
alWorkPoint.clear();
SaveFileDialog dialog = new SaveFileDialog();
 dialog.Title = "save";
 dialog.InitialDirectory = ".\\";
 dialog.FileName = "new.txt";
 dialog.Filter = "txt file (*.*)|*.txt";
 if (dialog.ShowDialog() == DialogResult.OK)
   {
       StreamWriter filew = new StreamWriter((dialog.FileName));
       DecoderListView();
       for (int i = 0; i < alWorkPoint.Count; i++)
       {
           filew.WriteLine(alWorkPoint[i].ToString());
       }
       filew.Close();
       MessageBox.Show("save ok");
   } 


//--------直接存檔 指定檔名


StreamWriter filew = new StreamWriter(("new.txt"));
filew.WriteLine("hello");
filew.WriteLine("world");
filew.WriteLine(DateTime.Now);
filew.Close();


測試環境:vs2015 c#.net



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

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