⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 这是一个简单的csharp程序。他能实现的功能也比较简单。
💻 CS
📖 第 1 页 / 共 3 页
字号:
            int returnValue;

            try
            {
                returnValue = command.ExecuteNonQuery();
            }
            catch
            {
                returnValue = -1;
            }
            
            return returnValue;
        }

        private int InsertQuery4(decimal NEW_ID)
        {
            System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
            command.Connection = trans.Connection;
            command.CommandText = "INSERT INTO NEW_TABLE4\r\n      (NEW_ID)\r\nVALUES (@NEW_ID); \r\nSELECT NEW_ID, NEW_JI" +
                "ANGLI, NEW_CHUFEN FROM NEW_TABLE4 WHERE (NEW_ID = @NEW_ID)";
            command.CommandType = CommandType.Text;
            command.Parameters.Add(new System.Data.SqlClient.SqlParameter("@NEW_ID", System.Data.SqlDbType.Decimal, 9, System.Data.ParameterDirection.Input, 18, 0, "NEW_ID", System.Data.DataRowVersion.Current, false, null, "", "", ""));

            command.Transaction = trans;

            command.Parameters[0].Value = ((decimal)(NEW_ID));
            
            int returnValue;

            try
            {
                returnValue = command.ExecuteNonQuery();
            }
            catch
            {
                returnValue = -1;
            }
            
            return returnValue;
        }

        //-------------------------------------------------------------------------
        //自定义函数,用于创建和写拷贝过程中的错误日志
        //访问类型: private
        //返回类型: void
        //参数类型: 1. String, 2. ArrayList
        //参数意义: 1. 要写入的文件名, 2. 要写入的数据
        //-------------------------------------------------------------------------
        //private void CreateLogFile(String fileName, ArrayList textToAdd)
        private void CreateLogFile(ArrayList textToAdd)
        {
            String strPath = GetFilePath(textBox1.Text);
            strPath = strPath.TrimEnd('\\');

            //获取要写入的数据条数
            int iErrorCount = textToAdd.Count;

            //如果没有要写入的数据则向其中填充"没错!"
            if (0 == iErrorCount)
            {
                textToAdd.Add("没错!");
            }

            //尝试进行文件写入
            try
            {
                //共需要生成( 写入条数/每个文件容纳条数 )个文件
                for (int i = 0; i < (iErrorCount / 10); i++)
                {
                    //获取当前的时间(精确到毫秒),并将无法作为文件标识符的":"替换成"-"
                    String strDate = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + "-" + (i + 1).ToString("000");
                    
                    //打开文件
                    using (StreamWriter sw = new StreamWriter(strPath + @"\" + strDate + ".Log"))
                    {
                        //写入当前时间以及文件头信息
                        sw.Write("当前时间: ");
                        sw.WriteLine(DateTime.Now);
                        sw.WriteLine("出现异常的表名,行ID, 异常信息如下");
                        sw.WriteLine("===================");

                        //开始记录写入
                        for (int j = 0; j < 10 && (i * 10 + j) < iErrorCount; j++)
                        {
                            sw.WriteLine(textToAdd[i * 10 + j].ToString());
                        }
                    }
                }
            }
            catch (Exception e)
            {
                //如果文件创建或打开出现异常,则提示
                MessageBox.Show(e.Message + "\n文件创建或打开出错!", "文件出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //-------------------------------------------------------------------------
        //自定义函数,用于获得写日志文件的路径
        //访问类型: private
        //返回类型: String
        //返回意义: 从ini文件获得的文件路径
        //参数类型: String
        //参数意义: ini文件所在位置
        //-------------------------------------------------------------------------
        private String GetFilePath(String IniFilePath)
        {
            StringBuilder sbFilePath = new StringBuilder(255);

            GetPrivateProfileString("Path", "FilePath", "", sbFilePath, 255, IniFilePath);

            return sbFilePath.ToString();
        }

        //文件路径选择按钮单击事件
        private void button2_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = "C:\\";
            folderBrowserDialog1.ShowDialog();
            textBox1.Text = folderBrowserDialog1.SelectedPath.TrimEnd('\\') + @"\" + "filepath.ini";
        }

        //-------------------------------------------------------------------------
        //自定义函数,用于检测filepath.ini文件是否存在
        //访问类型: private
        //返回类型: bool
        //返回意义: true: 文件存在, false: 没有选择文件路径或文件不存在
        //-------------------------------------------------------------------------
        private bool CheckIniFile()
        {
            //判断是否选择了ini文件路径
            if (textBox1.Text.Equals(""))
            {
                MessageBox.Show("请选择指定的ini文件!", "没有选择文件", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            //判断指定目录下是否存在filepath.ini文件
            if (!File.Exists(textBox1.Text))
            {
                //指定目录下不存在filepath.ini文件, 询问用户是否创建该文件
                if (MessageBox.Show("指定的目录下没有filepath.ini文件!\n是否需要系统为您创建filepath.ini文件?", "文件读取出错", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
                    == DialogResult.Yes)
                {
                    //将预设值写入ini文件
                    using (StreamWriter swIni = new StreamWriter(textBox1.Text))
                    {
                        swIni.WriteLine(@"[Path]");
                        swIni.Write(@"FilePath=");
                    }
                    //提示用户filepath.ini文件创建成功, 但需要用户自行输入Log文件存储路径
                    MessageBox.Show("filepath.ini文件已经创建, 请在其中填入日志文件的路径!", "请填路径", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    System.Diagnostics.Process.Start(textBox1.Text);

                    return false;
                }
                else
                {
                    return false;
                }
            }

            return true;
        }

        //-------------------------------------------------------------------------
        //自定义函数,用于检测filepath.ini文件中是否已经填写Log文件存储路径
        //访问类型: private
        //返回类型: bool
        //返回意义: true: 路径存在或已经创建, false: 路径不存在
        //-------------------------------------------------------------------------
        private bool CheckLogFilePath()
        {
            //获得Log文件存储路径
            String strPath = GetFilePath(textBox1.Text);
            strPath = strPath.TrimEnd('\\');

            //判断用户是否已经填写Log文件存储路径
            if (strPath.Equals(""))
            {
                MessageBox.Show("请在指定目录的filepath.ini文件中填入日志存储的路径!", "不存在的路径", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            //判断Log文件存储路径是否存在
            else if (!Directory.Exists(strPath))
            {
                char [] sDevice = new char[2];
                strPath.CopyTo(0, sDevice, 0, 2);
                if (!IsLocalDevice(sDevice[0].ToString() + sDevice[1].ToString()))
                {
                    MessageBox.Show("对不起, 您在filepath.ini文件中填写的Log文件存储路径有误(不是系统有效的物理盘符),请检查!",
                        "文件路径错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                //如果不存在则创建该路径
                Directory.CreateDirectory(strPath);
            }

            return true;
        }

        //-------------------------------------------------------------------------
        //自定义函数,用于检测filepath.ini文件中填写的Log文件存储路径是否是实际物理硬盘地址
        //访问类型: private
        //返回类型: bool
        //返回意义: true: 是物理硬盘地址, false: 不是物理硬盘地址
        //参数类型: String
        //参数意义: 要检测的字符串
        //-------------------------------------------------------------------------
        private bool IsLocalDevice(String sDevice)
        {
            //获得系统物理硬盘序列
            ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk WHERE DriveType=3");
            ManagementObjectCollection moc = mos.Get();

            //判断待检测字符串是否在物理硬盘序列中
            foreach (ManagementObject mo in moc)
            {
                if (sDevice.Equals(mo["Caption"].ToString()))
                {
                    return true;
                }
            }

            return false;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -