【发布时间】:2021-05-19 01:04:10
【问题描述】:
我编写代码来接收文本文件的路径并将其存储在我公开声明的字符串变量中。 然后我想知道文件是否存在使用
System.IO.File.Exists(pathoffile)
但即使有文件,它也总是返回 false。 然后当我尝试像这样直接添加字符串路径时
public string propertyfile = @"C:\Users\PFA Wongsawat\Desktop\part_no_and_path_list.txt"
功能
System.IO.File.Exists(pathoffile)
返回真
我已经检查了从文本文件中读取的接收路径(字符串)。通过切断“\n”和“\r”并使用trim()。但它仍然返回false。 我错过了什么吗?这两者有什么区别?。我对这个 c# 太陌生了。我很不擅长提前道歉。
这是我的代码
public string pathfromread, partnumber, pathfile, portname, partnofromserial,propertypathfile; //Declare Variables
public string propertyfile = @"C:\Users\PFA Wongsawat\Desktop\Properties.txt";
public string pathoffile ;
public string backuppath ;
public string pdffolderpath ;
private void propertyget()
{
if (File.Exists(propertyfile))
{
StreamReader readpropertyfile = new StreamReader(propertyfile);
string readproperty;
while ((readproperty = readpropertyfile.ReadLine()) != null)
{
string[] propertyfromread = readproperty.Trim().Split('=');
if (propertyfromread.GetValue(0).ToString() == "pathoffile")
{
pathoffile = propertyfromread.GetValue(1).ToString();
pathoffile = pathoffile.Replace("\n", "").Replace("\r", "");
MessageBox.Show(pathoffile, "path file");
}
else if ((propertyfromread.GetValue(0).ToString() == "backuppath"))
{
backuppath = propertyfromread.GetValue(1).ToString();
backuppath = backuppath.Replace("\n", "").Replace("\r", "");
MessageBox.Show(backuppath);
}
else if ((propertyfromread.GetValue(0).ToString() == "pdffolderpath"))
{
pdffolderpath = propertyfromread.GetValue(1).ToString();
pdffolderpath = pdffolderpath.Replace("\n", "").Replace("\r", "");
MessageBox.Show(pdffolderpath);
}
else if ((propertyfromread.GetValue(0).ToString() == "portname"))
{
portname = propertyfromread.GetValue(1).ToString();
portname = portname.Replace("\n", "").Replace("\r", "");
MessageBox.Show(portname);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
propertyget();
dv = dt.DefaultView; //set dv index count to != 0 to prevent error from null input when click on remove button
if (System.IO.File.Exists(pathoffile))//Check if file exist or not
{
}
else
{
try
{
MessageBox.Show("Database Text File Missing. Please Select New File", "Database Text File Missing", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
OpenFileDialog regispath = new OpenFileDialog();
regispath.Title = "Select Database Text File (part_no_and_path_list.txt)";
regispath.Multiselect = false;
regispath.Filter = "Text file (*.txt)|*.txt";
regispath.RestoreDirectory = true;
regispath.ShowDialog();
pathfile = regispath.FileName;
File.Copy(pathfile, pathoffile);
}
catch
{
这是我的属性文本文件
pathoffile=@"C:\Users\PFA Wongsawat\Desktop\part_no_and_path_list.txt"
backuppath=@"C:\Users\PFA Wongsawat\Documents\part_no_and_path_list.txt"
pdffolderpath=@"C:\Users\PFA Wongsawat\Downloads\"
portname=COM3
在这种情况下,结果总是一个消息框显示“缺少数据库文本文件。请选择新文件”
谢谢你,对不起我的英语不好。
【问题讨论】:
-
我猜你的格式/结构有误,打印出来看看区别
-
我建议您将每种情况下的值打印到控制台。您会看到,在工作案例中,没有双引号,也没有
@符号。这些是字符串的 C# 语法 的一部分。您的属性文件不是 C#。 (顺便说一句,我还建议阅读 .NET 命名约定,以及使用array[index]语法而不是调用GetValue的数组访问。) -
@JonSkeet 谢谢!如您所说,我将代码更改为使用 array[index] 。而且效果很好。
标签: c# file-io configuration load text-files