首页 新闻 会员 周边

C# 类中的方法调用该类的属性值出问题

0
悬赏园豆:15 [已解决问题] 解决于 2020-06-01 21:26

以下代码总是报错, 不知道是哪里的问题, 单独使用console.writeline, 读出来的文件地址是正确的;但若调用ta.Gettalltext 就报错

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace TestVS
{

class Program
{
    class Ta
    {
        private string txtFullName;
        public string TxtFullNamE
        {
            set
            {
                txtFullName = value;
            }
            get
            { return txtFullName; }

        }

        public void GetAlltext()//方法:读取文件中的所有内容
        {
            string str1 = File.ReadAllText(txtFullName);
            Console.WriteLine(str1);
        }


    }
     static void Main(string[] args)
    {
        var ta = new Ta();
        string fullName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
        string[] path = fullName.Split('\\');
        string fullpath = null;
        for (int i = 0; i < path.Length - 2; i++)
        {
            fullpath = fullpath + path[i] + @"\";
        }
        ta.TxtFullNamE = fullpath + @"a.txt";


        ta.GetAlltext();
   
        Console.ReadKey();
    }
}

}

问题补充:

string str1 = File.ReadAllText(txtFullName);
这一句报错, 报错内容请见图片

但用console.writeline(txtFullName) , 则显示: J:\Text\a.txt ,又是正确的, 这是哪用错了吗

小白沙的主页 小白沙 | 初学一级 | 园豆:105
提问于:2020-05-30 18:55

有这个文件吗?最好把异常信息贴出来。

blackheart 3年前

**\bin\a.txt ? 你确定是这个目录?不是bin\debug\a.txt?

blackheart 3年前
< >
分享
最佳答案
1

bin 目录下有a.txt 这个文件吗?
感觉错误提示提示的错误还是很明显的啊

收获园豆:10
Tom.汤 | 老鸟四级 |园豆:3028 | 2020-06-01 08:38

感谢提醒,的确是文件放错位置了

小白沙 | 园豆:105 (初学一级) | 2020-06-01 21:35
其他回答(3)
0

报什么错?

Timetombs | 园豆:3954 (老鸟四级) | 2020-05-30 20:23
0

你看一下,console.writeline的地址和报错的地址不一致

猝不及防 | 园豆:2781 (老鸟四级) | 2020-06-01 09:35
0

你把这个地址,在文件夹里打开看看,是不是找不到该文件

收获园豆:5
华临天下 | 园豆:1501 (小虾三级) | 2020-06-01 15:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册