如何读取其地址存储在变量中的文件。 [英] How to read a file whose address is stored in a variable.

查看:71
本文介绍了如何读取其地址存储在变量中的文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  string  text = System.IO.File.ReadAllText( @  D:\Selected Instances \Insertion graphs\1-FullIns_3.col); 



如果我们在FileName变量上存储文件路径,这是读取文件的方法。



我尝试过:



  string  text = System.IO.File.ReadAllText(@FileName) ; 



不工作

解决方案

我想的是



  string  filename =  @  D:\Selected Instances ...; 
if (!File.Exists(filename))
{
Console.WriteLine( 文件{0}不存在,filename);
Environment.Exit(-1);
}

string text = File.ReadAllText(filename);

// 用文字做点什么


你有没有试过

  string  fileName =  @  D:\Selected Instances \Insertion graphs\1-FullIns_3.col; 
string text = System.IO.File.ReadAllText(fileName);





@字符不能与变量一起使用,只能使用文字字符串。

参见字符串类(系统) [ ^ ]



Quote:

请注意,在C#中,因为反斜杠(\)是转义字符,所以必须转义字符串中的字面反斜杠或整个字符串必须为@ -quoted。


string text = System.IO.File.ReadAllText(@"D:\Selected Instances\Insertion graphs\1-FullIns_3.col");


this is the way to read a file what if we have file path stored on FileName variable.

What I have tried:

string text = System.IO.File.ReadAllText(@FileName);


not working

解决方案

I'd think about

string filename = @"D:\Selected Instances...";
if (!File.Exists(filename))
{
  Console.WriteLine("File {0} Doesnt Exist", filename);
  Environment.Exit(-1);
}

string text = File.ReadAllText(filename);

// Do something with text 


Have you tried

string fileName = @"D:\Selected Instances\Insertion graphs\1-FullIns_3.col";
string text = System.IO.File.ReadAllText(fileName);



The @ character cannot be used with a variable, only literal strings.
See String Class (System)[^]

Quote:

Note that in C#, because the backslash (\) is an escape character, literal backslashes in a string must be escaped or the entire string must be @-quoted.


这篇关于如何读取其地址存储在变量中的文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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