在 vb.net 中将当前目录与文件名结合 [英] Combine Current Directory with file name in vb.net

查看:29
本文介绍了在 vb.net 中将当前目录与文件名结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经测试了以下两个代码.第一个代码可以正常工作,因为我提供了完整的路径名,但第二个代码给出了找不到文件的错误.路径位置的值在两个代码中都显示相同案例.我想我没有正确地连接刺痛.我也试过给出没有扩展名的文件名.有什么帮助吗?

I have tested the following two codes .The first one works as I'm giving the full path name but the second one give the error saying the file is not found.The value of the path location is shown same in both the cases.I think i'm not concatenating the stings right.I have also tried to give the filename without the extension.Any help?

第一个代码

    Dim content As String
    Dim path As String
    path = "C:\Users\****\bin\Debug\tns.txt"
    MessageBox.Show(path)
    Try
        Dim sr As New StreamReader(path)
        content = sr.ReadToEnd()
        Console.WriteLine(content)
        MessageBox.Show(content)
    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString())
    End Try

第二个代码

 Dim content As String
    Dim path As String
    path = Directory.GetCurrentDirectory() + "\tns.txt"
    MessageBox.Show(path)
    Try
        Dim sr As New StreamReader(path)
        content = sr.ReadToEnd()
        Console.WriteLine(content)
        MessageBox.Show(content)
    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString())
    End Try

推荐答案

path = IO.Path.Combine(Directory.GetCurrentDirectory(), "tns.txt")

这篇关于在 vb.net 中将当前目录与文件名结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆