积极答复者
请教一个关于文件读取的问题!

问题
答案
-
如果是当前程序的目录的话,建议使用:
private static string _appPath = string.Empty;
/// <summary>
/// 程序的目录
/// </summary>
private static string AppPath
{
get
{
if (string.IsNullOrEmpty(_appPath))
{
if (System.Environment.OSVersion.Platform != PlatformID.WinCE)
_appPath="";//指向你的WINFORM程序路径
else
_appPath = typeof(Program).Assembly.GetModules()[0].FullyQualifiedName.Replace(typeof(Program).Assembly.GetModules()[0].Name, "");//指向WINCE的程序径.其中的Program就是程序的类名了.
}
return _appPath;
}
}- 已标记为答案 Guang-Ming Bian - MSFT 2010年1月5日 6:45
2010年1月3日 7:07 -
首先感谢 EVANLEE的热心回答,谢谢了。
今天早上人比较累,一时没想明白,所以就来这里问问。
刚又查阅了一下MSDN,发现里面已经有了答案。
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、由于在 Pocket PC 应用程序中不存在固有的当前目录设置,因此在代码中指定文件名而不使用路径规范进行指定会返回一个 FileNotFoundException。Pocket PC 应用程序将数据文件与您的程序集文件一起存储在 \Program Files\myAssembly\(其中 myAssembly 为程序集名称)下。
示例
此示例演示如何通过以下方法确定当前运行的应用程序的路径:获取当前执行的程序集的完全限定目录名称并为其附加该应用程序的文件名。注意,如果应用程序在设备的根目录中运行,则返回的路径信息为一个空字符串。
Visual Basic 复制代码
Dim strAppDir As String = Path.GetDirectoryName( _ Assembly.GetExecutingAssembly().GetName().CodeBase) Dim strFullPathToMyFile As String = Path.Combine(strAppDir, "myFileName.txt") MessageBox.Show(String.Format("Path to the application is: '{0}'." + _ "Full path to the file in the application folder is: '{1}'", _ strAppDir, strFullPathToMyFile))
C# 复制代码
String strAppDir = Path.GetDirectoryName( Assembly.GetExecutingAssembly().GetName().CodeBase); String strFullPathToMyFile = Path.Combine(strAppDir, "fileName.txt"); MessageBox.Show(String.Format("Path to the application is: '{0}'." + "Full path to the file in the application folder is: '{1}'", strAppDir, strFullPathToMyFile));
- 已标记为答案 newpeilan 2010年1月3日 16:35
2010年1月3日 7:40
全部回复
-
如果是当前程序的目录的话,建议使用:
private static string _appPath = string.Empty;
/// <summary>
/// 程序的目录
/// </summary>
private static string AppPath
{
get
{
if (string.IsNullOrEmpty(_appPath))
{
if (System.Environment.OSVersion.Platform != PlatformID.WinCE)
_appPath="";//指向你的WINFORM程序路径
else
_appPath = typeof(Program).Assembly.GetModules()[0].FullyQualifiedName.Replace(typeof(Program).Assembly.GetModules()[0].Name, "");//指向WINCE的程序径.其中的Program就是程序的类名了.
}
return _appPath;
}
}- 已标记为答案 Guang-Ming Bian - MSFT 2010年1月5日 6:45
2010年1月3日 7:07 -
首先感谢 EVANLEE的热心回答,谢谢了。
今天早上人比较累,一时没想明白,所以就来这里问问。
刚又查阅了一下MSDN,发现里面已经有了答案。
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、由于在 Pocket PC 应用程序中不存在固有的当前目录设置,因此在代码中指定文件名而不使用路径规范进行指定会返回一个 FileNotFoundException。Pocket PC 应用程序将数据文件与您的程序集文件一起存储在 \Program Files\myAssembly\(其中 myAssembly 为程序集名称)下。
示例
此示例演示如何通过以下方法确定当前运行的应用程序的路径:获取当前执行的程序集的完全限定目录名称并为其附加该应用程序的文件名。注意,如果应用程序在设备的根目录中运行,则返回的路径信息为一个空字符串。
Visual Basic 复制代码
Dim strAppDir As String = Path.GetDirectoryName( _ Assembly.GetExecutingAssembly().GetName().CodeBase) Dim strFullPathToMyFile As String = Path.Combine(strAppDir, "myFileName.txt") MessageBox.Show(String.Format("Path to the application is: '{0}'." + _ "Full path to the file in the application folder is: '{1}'", _ strAppDir, strFullPathToMyFile))
C# 复制代码
String strAppDir = Path.GetDirectoryName( Assembly.GetExecutingAssembly().GetName().CodeBase); String strFullPathToMyFile = Path.Combine(strAppDir, "fileName.txt"); MessageBox.Show(String.Format("Path to the application is: '{0}'." + "Full path to the file in the application folder is: '{1}'", strAppDir, strFullPathToMyFile));
- 已标记为答案 newpeilan 2010年1月3日 16:35
2010年1月3日 7:40