简介: 在这篇文章记录了在C sharp 编程中使用IniParse 对ini配置文件进行读写的操作. 读取ini文件中的Mysql数据库配置文件进行数据库连接: 如果你想从一个 .ini 文件中读取配置信息,可以使用第三方库,如 IniParser。以下是如何使用 IniParser 库来实现: 首先,你需要使用 NuGet 包管理器安装 IniParser。在 Visual Studio 中,可以通过 NuGet 包管理器控制台执行以下命令:Install-Package IniParser 然后,创建一个 config.ini 文件,并添加以下内容: [Database] Server=your_server User=your_user Password=your_password Database=your_database 接着,修改你的 mysqldata 类如下: using System; using IniParser; using IniParser.Model; using MySql.Data.MySqlClient; using Sunny.UI; namespace ExpressManageSystem { internal class mysqldata { private static readonly string configFile = "config.ini"; private static readonly FileIniDataParser parser = new FileIniDataParser(); private static readonly IniData configData = parser.ReadFile(configFile); public static string connection_string { get { var databaseSection = configData["Database"]; return $"server={databaseSection["Server"]};user={databaseSection["User"]};password={databaseSection["Password"]};database={databaseSection["Database"]}"; } } public MySqlConnection mySqlConnection = new MySqlConnection(connection_string); //连接数据库 public bool connect_db() { try { mySqlConnection.Open(); return true; } catch (Exception ex) { UIMessageBox.Show(ex.Message); return false; } } //关闭数据库链接 public bool close_db() { try { mySqlConnection.Close(); return true; } catch (Exception ex) { UIMessageBox.Show(ex.Message); return false; } } } } 在这个修改后的代码中,使用了 IniParser 库来读取 config.ini 文件中的配置信息。这样,你就可以通过修改 config.ini 文件来更改数据库连接信息,而无需修改代码。 读取ini文件中的信息保存到变量中: 请参考示例代码 private void CopyExpressInfo() { try { if (datagridview_main_information.SelectedRows.Count == 0) { MessageBox.Show("请先选择客户"); return; } // 获取选中的行 DataGridViewRow selectedRow = datagridview_main_information.SelectedRows[0]; // 获取选中行的数据 string customercode = selectedRow.Cells["customercode"].Value.ToString(); //所需要复制的内容 var configFile = "config.ini"; //定义ini配置文件名称 var parser = new FileIniDataParser(); IniData configData = null; if (File.Exists(configFile)) { configData = parser.ReadFile(configFile); //读取CompanyInfo 部分的信息 var companyInfoSection = configData["CompanyInfo"]; string receiverAir = companyInfoSection["Receiver_air"]; string addressAir = companyInfoSection["Address_air"].ToString(); string phoneNumberAir = companyInfoSection["Phonenumber_air"]; string receiverSea = companyInfoSection["Receiver_sea"]; string addressSea = companyInfoSection["Address_sea"].ToString(); string phoneNumberSea = companyInfoSection["Phonenumber_sea"]; string cnShippinginfo = $"收件人:{receiverAir}\r\n收货地址:{addressAir}({customercode})+空运部\r\n手机号:{phoneNumberAir} \n" + $" \r\n收件人:{receiverSea} \r\n收货地址:{addressSea}({customercode})+海运部 \r\n手机号:{phoneNumberSea}"; //保存到粘贴板 Clipboard.SetText(cnShippinginfo); MessageBox.Show($"已成功复制地址信息到剪贴板。"); } else { MessageBox.Show("配置文件 config.ini 不存在,系统正在为你重新创建!"); configData = new IniData(); MessageBox.Show("创建config.ini 成功,请在软件根目录填写相关信息!"); } } catch (Exception ex) { MessageBox.Show($"Error: {ex.Message}"); } }

March 18, 2024 0comments 60hotness 0likes Read all

缘由 好久没给网吧维护了,最近给日本网吧维护的时候, 由于要在工作站开机启动之后执行一些初始化程序,要用到一个延迟启动程序。(程序是前辈写的,帮了很多忙。鞠躬)但是在日本语系统下无法正常运行 报错如下: 如果要解决该错误,需要在区域选项中,将程式区域改为中国即可。如此一来会产生一个新的问题, 某些程式会用区域来判断用户的location, 会导致一些软件无法正常运行。 于是我依葫芦画瓢,手搓了一个能在日本语系统下正常运行的延迟启动工具 功能和用法 简介 该延迟启动工具的本体只有一个exe执行文件和ini配置文件 执行文件会读取ini文件中的配置,然后执行相关任务 配置文件说明 配置文件包含TargetPath,FileExtensions,Timeout选项 TargetPath: 所需要执行的目录路径(绝对路径) FileExtenions: 需要执行的文件格式 Timeout: 延迟时间 毫秒为单位 用法 在给工作站开超级时,将Hiderun.exe 添加到系统启动项 根据你自身需求,在配置文件中“config.ini”填写相对应的路径和文件格式,以及时间。 日志 程序会在程序根目录生成log文件记录执行状态和报错,遇到问题可以在日志中查看明细。

December 6, 2023 0comments 36hotness 0likes Read all

What is Classes in C#? Objects Access Modifiers https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/access-modifiers https://www.cnblogs.com/xinaixia/p/5775471.html Code using System; //class declaration public class Car { //instance 6 variables String name; String color; String transmission; String bodytype; int seat; int year; //constructor declaration of class public Car(String name, String color, String transmission, String bodytype, int seat, int year) { this.name = name; this.color = color; this.transmission = transmission; this.bodytype = bodytype; this.seat = seat; this.year = year; } //property 6 public String getName() { return name; } public String getColor() { return color; } public String getTransmission() { return transmission; } public String getType() { return bodytype; } public int getSeat() { return seat; } public int getYear() { return year; } //method public String ToString() { return ("This is Car detail- CarName:" + this.getName() + "\n Color :" + this.getColor() + "\n Transmission: " + this.getTransmission() + "\n Body type: " + this.getType() + "\n Seat : " + this.getSeat() + "\n Year: " + this.getYear())+"\n ================="; } //main method public static void Main(string[] args) { // 5 object Car Toyota = new Car("Toyota Rush", "Silver", "Auto", "SUV", 7, 2020); Car Bmw = new Car("BMX X1", "Silver", "Auto", "SUV", 7, 2022); Car Honda = new Car("Accord", "Black", "Auto", "Sedan", 4, 2016); Car Tesla = new Car("Tesla S", "Red", "Auto", "Sedan", 4, 2017); Car Mazda = new Car("Mazda 929 Legato", "White", "Manual", "Sedan", 4, 1980); //output Console.WriteLine(Toyota.ToString()); Console.WriteLine(Bmw.ToString()); Console.WriteLine(Honda.ToString()); Console.WriteLine(Tesla.ToString()); Console.WriteLine(Mazda.ToString()); Console.WriteLine("==============\n Thank you !"); //out put date and time DateTime now = DateTime.Now; Console.WriteLine("Print date: " + now); Console.ReadLine(); } }

October 20, 2022 0comments 37hotness 0likes Read all

IDEA User input data Convert to index Using index number fund element vaule Output element vaule Screenshot Code using System; namespace array2d { public class array2d { public static void Main(string[] args) { //initilizing arrays string[,] platenumber = new string[5,3] { {"ZJI163","USA1008","PHA101"}, {"PJ130108", "USA1006","JAP118" }, {"MAX168", "USA1002" ,"TWD120"}, { "AZZ9756", "USA1005","KOR119" }, {"NAS1223","USA109","DDT886"} }; //wait for user input,convert to int Console.WriteLine("Welcome use DK parking system!\n Solt: 0-4 \n floor: 0-2"); Console.WriteLine("Plz type slot: "); int solt = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Plz type floor: "); int floor = Convert.ToInt32(Console.ReadLine()); // decision the length of the element if (solt < 5 && floor < 3) { string car = platenumber[solt, floor]; Console.WriteLine("Found platenumber is :" + car); } else { Console.WriteLine("\nThe value you entered does not exist"); } Console.WriteLine("Thank you !"); Console.ReadLine(); } } }

October 16, 2022 0comments 34hotness 0likes Read all

C# Multidimensional Arrays As discussed in the previous chapter, Arrays in C# will support multi-dimensional arrays. In c#, a multidimensional array is an array that contains more than one dimension to represent the elements in a tabular format like rows and columns.In c#, multidimensional arrays can support either two or three-dimensional series. To create multi-dimensional arrays, we need to use a comma (,) separator inside the square brackets. C# Multi-Dimensional Array Declaration In c#, Multidimensional Arrays can be declared by specifying the data type of elements followed by the square brackets [] with comma (,) separator. The following are examples of creating two or three-dimensional arrays in the c# programming language. Question: Create a complete C# program that will compute the average of all elements in array x int [,] x = new int[4, 5] { { 4, 5, 6, 2, 12 }, { 10, 25, 33, 22, 11 },{ 211, 32, 43, 54, 65 }, { 3, 2, 1, 5, 6 } }; Average Solution Use for loop get array x elements values Add values Divide results by no. of values Code using System; namespace Module7 { public class Module7 { public static void Main(string[] args) { //initilizing array int[,] x = new int[4, 5] { { 4, 5, 6, 2, 12 }, { 10, 25, 33, 22, 11 }, { 211, 32, 43, 54, 65 }, { 3, 2, 1, 5, 6 } }; Console.WriteLine("---Two dimensional array elements---"); // use for loop get array x elements for(int y=0; y<4; y++) { int sum = 0; double avg =…

October 4, 2022 0comments 34hotness 0likes Read all

Question: Create a complete C# program that will simulate a log-in process. If the username and password are both correct the program will display “Log-in Successful”, otherwise it will display “Invalid Log-in details” FlowChart: C# Logical Operators: Code Answer: using System; namespace Module6 { class module6 { static void Main(string[] args) { string emailid, password; //use readline get user input Console.WriteLine("Plz enter u Emaill address :"); emailid = Console.ReadLine(); Console.WriteLine("Plz enter u Password"); password = Console.ReadLine(); //use if else to decision input data contains value if (emailid.Contains("[email protected]") && (password.Contains("d123456"))) { Console.WriteLine("Your Log-in Was Successful!"); } else { Console.WriteLine("Invalid Log-in details"); } Console.ReadLine(); } } } Reference C# Logical Operators with Exampleshttps://www.tutlane.com/tutorial/csharp/csharp-logical-operators-with-examples C# String Contains()https://www.javatpoint.com/csharp-string-contains

October 3, 2022 0comments 43hotness 0likes Read all

Example explainedLine 1: using System means that we can use classes from the System namespace. Line 2: A blank line. C# ignores white space. However, multiple lines makes the code more readable. Line 3: namespace is used to organize your code, and it is a container for classes and other namespaces. Line 4: The curly braces {} marks the beginning and the end of a block of code. Line 5: class is a container for data and methods, which brings functionality to your program. Every line of code that runs in C# must be inside a class. In our example, we named the class Program. Don't worry if you don't understand how using System, namespace and class works. Just think of it as something that (almost) always appears in your program, and that you will learn more about them in a later chapter. Line 7: Another thing that always appear in a C# program, is the Main method. Any code inside its curly brackets {} will be executed. You don't have to understand the keywords before and after Main. You will get to know them bit by bit while reading this tutorial. Line 9: Console is a class of the System namespace, which has a WriteLine() method that is used to output/print text. In our example it will output "Hello World!". If you omit the using System line, you would have to write System.Console.WriteLine() to print/output text. Note: Every C# statement ends with a semicolon ;. Note: C# is case-sensitive: "MyClass" and "myclass" has different meaning. Note: Unlike Java, the…

September 24, 2022 0comments 35hotness 0likes Read all