在 C# 中,如何访问设置范围之外的变量?
In C# how can I access a variable outside of the scope where it was set?
我尝试创建一个函数来打开配置文件,读取特定行以获取存储的值,然后 return 将该值发送给调用子程序。因为我试图在 while 循环之外 return 它,所以我遇到了编译错误。我将如何获取存储值并将其设置为我可以在我的命名空间中的其他地方使用的变量?
public class Functions
{
public static string GetODVPath()
{
//Read stored path of ODV.accdb file
string fileName = @"C:\Users\" + Environment.UserName + @"\ODV.conf";
using (StreamReader reader = new StreamReader(fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
//if line length isnt 0 and line starts with [odv] set value of path
string odvPath = (line.Length != 0 && line.StartsWith("[ODV]")) ? line.Substring(4) : "";
}
return odvPath();
}
}
}
这不就是你需要的吗?
class YourApp
{
static void Main(string[] args)
{
string odvPath = Functions.GetODVPath();
// Do whatever you wish with that
}
}
public class Functions
{
public static string GetODVPath()
{
string fileName = @"C:\Users\" + Environment.UserName + @"\ODV.conf";
// If you not sure that file always will be at its place
if (!File.Exists(fileName))
{
return string.Empty;
}
using (StreamReader reader = new StreamReader(fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (line.StartsWith("[ODV]")))
{
return line.Substring(4);
}
}
}
return string.Empty;
}
}
这种方法 returns 一个值,如果找到,或者空字符串,如果没有。
事实上 Functions
class 及其方法 GetODVPath
是 public
- 它们可以在命名空间的任何地方调用。
已编辑:line.Length
检查可能会被删除,因为如果 line.StartsWith("[ODV]")
是 true
- 它至少有 4 个字符长度,下一个 Substring(4) 调用将只替换“ [ODV]" 找到的行的一部分。
还添加了一个 File.Exists 检查,这在文件可能不存在时很有用。
此外,恕我直言,可以使用 System.Linq
Where
扩展方法进行简化:
public static string GetODVPath()
{
// Changed with SpecialFolder enum
string fileName = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\ODV.conf";
// If you not sure that file always will be at its place
if (!File.Exists(fileName)) // Don't forget to add System.IO reference
{
return string.Empty;
}
// Where filtration looks much simple
string result = File.ReadAllLines(fileName).Where(line => line.StartsWith("[ODV]")).FirstOrDefault();
// Here is where ternar is in right place :)
return string.IsNullOrEmpty(result) ? string.Empty : result.Substring(4);
}
如果你想在外部使用这个变量class:
class YourApp
{
static void Main(string[] args)
{
var generateOdvPath = new Functions();
generateOdvPath.GetODVPath();
string odvPath = generateOdvPath.GeneralOdvPath;
}
}
public class Functions
{
public string GeneralOdvPath { get; set; }
public void GetODVPath()
{
//Read stored path of ODV.accdb file
string fileName = @"C:\Users\" + Environment.UserName + @"\ODV.conf";
using (StreamReader reader = new StreamReader(fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
//if line length isnt 0 and line starts with [odv] set value of path
string odvPath = (line.Length != 0 && line.StartsWith("[ODV]")) ? line.Substring(4) : "";
}
}
GeneralOdvPath = odvPath;
}
}
删除静态词并为您的路径声明一个礼节。
我尝试创建一个函数来打开配置文件,读取特定行以获取存储的值,然后 return 将该值发送给调用子程序。因为我试图在 while 循环之外 return 它,所以我遇到了编译错误。我将如何获取存储值并将其设置为我可以在我的命名空间中的其他地方使用的变量?
public class Functions
{
public static string GetODVPath()
{
//Read stored path of ODV.accdb file
string fileName = @"C:\Users\" + Environment.UserName + @"\ODV.conf";
using (StreamReader reader = new StreamReader(fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
//if line length isnt 0 and line starts with [odv] set value of path
string odvPath = (line.Length != 0 && line.StartsWith("[ODV]")) ? line.Substring(4) : "";
}
return odvPath();
}
}
}
这不就是你需要的吗?
class YourApp
{
static void Main(string[] args)
{
string odvPath = Functions.GetODVPath();
// Do whatever you wish with that
}
}
public class Functions
{
public static string GetODVPath()
{
string fileName = @"C:\Users\" + Environment.UserName + @"\ODV.conf";
// If you not sure that file always will be at its place
if (!File.Exists(fileName))
{
return string.Empty;
}
using (StreamReader reader = new StreamReader(fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (line.StartsWith("[ODV]")))
{
return line.Substring(4);
}
}
}
return string.Empty;
}
}
这种方法 returns 一个值,如果找到,或者空字符串,如果没有。
事实上 Functions
class 及其方法 GetODVPath
是 public
- 它们可以在命名空间的任何地方调用。
已编辑:line.Length
检查可能会被删除,因为如果 line.StartsWith("[ODV]")
是 true
- 它至少有 4 个字符长度,下一个 Substring(4) 调用将只替换“ [ODV]" 找到的行的一部分。
还添加了一个 File.Exists 检查,这在文件可能不存在时很有用。
此外,恕我直言,可以使用 System.Linq
Where
扩展方法进行简化:
public static string GetODVPath()
{
// Changed with SpecialFolder enum
string fileName = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\ODV.conf";
// If you not sure that file always will be at its place
if (!File.Exists(fileName)) // Don't forget to add System.IO reference
{
return string.Empty;
}
// Where filtration looks much simple
string result = File.ReadAllLines(fileName).Where(line => line.StartsWith("[ODV]")).FirstOrDefault();
// Here is where ternar is in right place :)
return string.IsNullOrEmpty(result) ? string.Empty : result.Substring(4);
}
如果你想在外部使用这个变量class:
class YourApp
{
static void Main(string[] args)
{
var generateOdvPath = new Functions();
generateOdvPath.GetODVPath();
string odvPath = generateOdvPath.GeneralOdvPath;
}
}
public class Functions
{
public string GeneralOdvPath { get; set; }
public void GetODVPath()
{
//Read stored path of ODV.accdb file
string fileName = @"C:\Users\" + Environment.UserName + @"\ODV.conf";
using (StreamReader reader = new StreamReader(fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
//if line length isnt 0 and line starts with [odv] set value of path
string odvPath = (line.Length != 0 && line.StartsWith("[ODV]")) ? line.Substring(4) : "";
}
}
GeneralOdvPath = odvPath;
}
}
删除静态词并为您的路径声明一个礼节。