这里我有这样的代码库:
class Program
{
static void Main(string[] args)
{
try
{
'String str;
'str = Server.MapPath("/financila_csharp");
StreamReader reader = new StreamReader("selectedmdx.txt");
StreamWriter writer = new StreamWriter("selectedxmlmdx.txt");
string line = reader.ReadLine();
while (line != null)
{
XmlDocument dom = new XmlDocument();
dom.LoadXml("<Result>" + System.Security.SecurityElement.Escape(line) + "</Result>");
writer.WriteLine(dom.DocumentElement.OuterXml);
line = reader.ReadLine();
}
Console.WriteLine("Completed");
reader.Close();
writer.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
在控制台窗口中显示“指定文件不存在”,即使我在同一项目目录中有“selectedmdx.txt”文件。
我该如何解决?
答案 0 :(得分:2)
我认为这是一个非Web应用程序。 所以试试
str = System.IO.Path.GetFullPath("/financila_csharp");
它将完美地运作
答案 1 :(得分:0)
Server.MapPath适用于Web应用程序而非控制台应用程序。
有关在控制台应用程序中获取路径的信息,请参阅this问题。
答案 2 :(得分:0)
文件“selectedmdx.txt”是否已添加到visual studio解决方案中?如果是,请在解决方案探索中选择它,然后按F4。在属性窗口中,将“复制到输出目录”设置为“始终复制”或“如果更新则复制”,以更适合您的方式为准。这会将文件复制到实际运行代码的输出目录。
希望这会有所帮助......
Vamyip