C# Ado.net 笔记 winfrom 基本使用方法

查截取字符某串个字符分割的最后一部分

 string s1 = "123.456.789";
 string s2 = string.Empty;
 //先求出最后出现这个字符的下标
 int index = s1.LastIndexOf('.');
 //从下一个索引开始截取
 s2=s1.Substring(index+1);
 Console.WriteLine(s2);
 Console.ReadLine();

 

 

查找文件是否存在

if (!Directory.Exists(path)) //判断是否存在某个文件夹
  {//不存在
    Directory.CreateDirectory(path); //创建文件夹
 }
if (!File.Exists(filePath)) //判断是否存在某个文件
  {
    FileStream fs = File.Create(filePath);//创建文件
    File.Delete(realInvImgPath);//删除文件
    fs.Close(); 
  }

写入日志方法

public static void TongjCheckFapWriter(String msg)
        {
            string TongjPath = AppDomain.CurrentDomain.BaseDirectory + "Logs\\Info\\CheckFap.txt";
            //写入日志
            using (StreamWriter writer = new StreamWriter(TongjPath, true))
            {
                writer.WriteLine(DateTime.Now.ToString() + ":" + msg);
                writer.Close();
            }
        }

选择浏览文件

          OpenFileDialog ofd = new OpenFileDialog();

                ofd.Filter = "Excel文件(*.xlsx;*.xls)|*.xlsx;*.xls|所有文件|*.*";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    txtpath.Text = ofd.FileName;
                }

 

导入excel

        private void input()
        {
            string filePath = txtpath.Text.Trim();

            if (filePath != null && File.Exists(filePath) == true)
            {
                IWorkbook wk = null;
                string extension = System.IO.Path.GetExtension(filePath);
                try
                {
                    FileStream fs = File.OpenRead(filePath);
                    if (extension.Equals(".xls"))
                    {
                        wk = new HSSFWorkbook(fs);
                    }
                    else
                    {
                        wk = new XSSFWorkbook(fs);
                    }
                    fs.Close();
                    List<WbInfo> WbInfoList = new List<WbInfo>();
                    int j = wk.NumberOfSheets;


                    for (int k = 0; k < j - 1; k++)
                    {
                        ISheet sheet = wk.GetSheetAt(k);
                        IRow row = sheet.GetRow(0);

                        string sheetname = wk.GetSheetName(k);

                        for (int i = 1; i <= sheet.LastRowNum - 1; i++)//LastRowNum 是当前表的总行数-1
                        {
                            WbInfo wbdeatil = new WbInfo();
                            row = sheet.GetRow(i);

                            for (int c = 0; c < row.LastCellNum; c++)
                            {
                                ICell cell = row.GetCell(c);
                                if (cell == null) //如果单元格为空时,程序会报错,这里判断提示用户,用try catch防止程序蹦
                                {
                                    MessageBox.Show(string.Format("第{0}行,第{1}列单元格为空!", i, c));
                                }
                                else
                                {
                                
                                    wbdeatil.a= row.GetCell(1).ToString();
                                    
                                    DateTime date = cell.DateCellValue;//获取时间类型
                                    
                                    int jine=cell.NumericCellValue;//数字类型
                                                                        
                                }
                            }
                            
                            WbInfoList.Add(wbdeatil);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Console.WriteLine(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("excel路径错误,请选择正确路径");
            }
            
        }

随机数  

Random rd = new Random();
int i = rd.Next();

  

打开exe程序

System.Diagnostics.Process.Start(Application.StartupPath + "\\XX.exe").WaitForExit();

调用exe并传入参数

System.Diagnostics.Process.Start(Application.StartupPath +"\\XX.exe", "参数1 参数2");

调用exe并传入参数并返回结果

 Process p = new Process();
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = Application.StartupPath + "\\XX.exe";
 p.StartInfo.CreateNoWindow = true;
 p.StartInfo.Arguments = "参数一 参数二 参数三";//传入参数,没有传空,空格分割
 p.Start();
 p.WaitForExit();
 string output = p.StandardOutput.ReadToEnd();
 int exitCode = p.ExitCode;
 MessageBox.Show(exitCode.ToString());

exe被调用

static void Main(string[] args)
{
if (args.Length > 0)
{
string canshu1 = args[0];
string canshu2 = args[1];
MessageBox.Show(canshu1);
MessageBox.Show(canshu2);
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

文件夹所在地址    

string logPath = AppDomain.CurrentDomain.BaseDirectory + "\\XX.txt";//文件夹所在地址

读取txt全部内容    

System.IO.File.AppendText //附加文本到最后
System.IO.File.ReadAllText(Path, System.Text.Encoding.Default) //读取所有文本内容

 删除某一个目录中所有的txt文件

string filePath = AppDomain.CurrentDomain.BaseDirectory + "data";
string[] strFileName = Directory.GetFiles(filePath, "*.txt");
foreach (var item in strFileName)
{
   File.Delete(item);
}
Console.ReadLine();

判断是否为空&保留两位小数

bool a = String.IsNullOrEmpty("111"); //判断是否为空
if (!a) { string b = Convert.ToDecimal(123).ToString("0.00");//保留两位小数 }

将子窗在父窗口居中 

 form1.StartPosition = FormStartPosition.CenterScreen;

DataGridView

(string)dgvgongxg.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;//获取当前选中单元格的行号和列号的值

dataGridView1.Row[i].Cells[j].Value;//获得某个(指定的)单元格的值:

dataGridView1.SelectedRows.Count;//获得选中的总行数:

dataGridView1.CurrentRow.Index;//获得当前选中行的索引

dataGridView1.CurrentCell.Value;//获得当前选中单元格的值: 

dataGridView1.SelectedCells(0).Value.ToString 取当前选择单元内容

string[] str = new string[dataGridView.Rows.Count];
for(int i;i<dataGridView1.Rows.Count;i++)
{
 if(dataGridView1.Rows[i].Selected == true)
  {
      str[i] = dataGridView1.Rows[i].Cells[1].Value.ToString();//取选中行的数据
  }
}
posted @ 2020-01-19 14:50  会跳舞的猪~  阅读(199)  评论(0编辑  收藏  举报