问题描述
1.这是一个代码行:
StreamReader f = new StreamReader(cbIzborFile.Text +。txt);
2.这是错误信息:
FileNotFoundException未处理
无法找到文件'C:\ Users \ user \ Documents \ Visis Studio 2010 \Projects \ Rad sa tekstualnim fajlom \Rad sa tekstualnim fajlom\bin\Debug \Rad sa tekstualnim fajlom.vshost.exe.txt'。
类型:System.IO.File.NotFoundException
问题:
1.如何消除上述错误?
2.如何将文本文件放入目录调试我的应用程序或引用我的应用程序中使用的文本文件的完整路径?
1.This is a code line:
StreamReader f=new StreamReader (cbIzborFile.Text+".txt");
2.This is Error message:
FileNotFoundException was unhandled
Could not find file 'C:\Users\user\Documents\Visual Studio 2010\Projects\Rad sa tekstualnim fajlom\Rad sa tekstualnim fajlom\bin\Debug\Rad sa tekstualnim fajlom.vshost.exe.txt'.
Type: System.IO.File.NotFoundException
Questions:
1.How do I eliminate mentioned error?
2.How to put textual files into directory Debug of my application or cite complete path of a textual files for using in my application?
推荐答案
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace NumberFiles
{
static class Program
{
/// <summary>
/// This is the MainForm of the application. For demonstration purposes this Form is "designed" "in code" - normally you would use
/// the visual Forms-Designer to create your Form.
/// </summary>
public class MainForm : Form
{
ListBox m_lbNumbers;
Label m_labelResult;
public MainForm()
{
// Adjust some Form-Properties
Width = 640;
Height = 480;
// Create a ToolStrip to let the user trigger the needed functions
ToolStrip ts = new ToolStrip();
// ... with some ToolButtons
ToolStripButton buttonOpenFile = new ToolStripButton { Text = "Open File" };
buttonOpenFile.Click += buttonOpenFile_Click;
ToolStripButton buttonGetMaximumNumber = new ToolStripButton { Text = "Max. Nr." };
buttonGetMaximumNumber.Click += buttonGetMaximumNumber_Click;
ToolStripButton buttonCountMaximumNumber = new ToolStripButton { Text = "Count Max. Nr." };
buttonCountMaximumNumber.Click += buttonCountMaximumNumber_Click;
ToolStripButton buttonCalculateAverage = new ToolStripButton { Text = "Calculate Average" };
buttonCalculateAverage.Click += buttonCalculateAverage_Click;
ToolStripButton buttonGetMaximumNumberOfSelectedNumbers = new ToolStripButton { Text = "Max. Nr. from Selection" };
buttonGetMaximumNumberOfSelectedNumbers.Click += buttonGetMaximumNumberOfSelectedNumbers_Click;
ts.Items.AddRange(new ToolStripItem[] { buttonOpenFile, buttonGetMaximumNumber, buttonCountMaximumNumber, buttonCalculateAverage, buttonGetMaximumNumberOfSelectedNumbers });
// Create a ListBox to show the numbers (with enabled multi selection)
m_lbNumbers = new ListBox { Dock = DockStyle.Fill, SelectionMode = SelectionMode.MultiExtended };
// Create a Label to show the calculation results (with some initial text)
m_labelResult = new Label { Dock = DockStyle.Bottom };
// Add the Controls to this Form (order is important for the docking logic!)
Controls.Add(m_lbNumbers);
Controls.Add(ts);
Controls.Add(m_labelResult);
}
void buttonOpenFile_Click(object sender, EventArgs e)
{
// Let the user choose a file
OpenFileDialog ofd = new OpenFileDialog { Title = "Select a file containing numbers" };
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// Read all lines from the text file selected by the user containing the numbers
string[] astrLines = File.ReadAllLines(ofd.FileName);
// Delete all current numbers from the list
m_lbNumbers.Items.Clear();
// Add the just read new numbers to the list
foreach (string strLine in astrLines)
m_lbNumbers.Items.Add(Convert.ToInt32(strLine));
}
}
// We use some LINQ for the calculations to make our live easy :-)
void buttonGetMaximumNumber_Click(object sender, EventArgs e)
{
IEnumerable<int> numbers = m_lbNumbers.Items.Cast<int>();
int iMaxNr = numbers.Max();
m_labelResult.Text = String.Format("Maximum number was {0}", iMaxNr);
}
void buttonCountMaximumNumber_Click(object sender, EventArgs e)
{
IEnumerable<int> numbers = m_lbNumbers.Items.Cast<int>();
int iMaxNr = numbers.Max();
int iCount = numbers.Count(n => n == iMaxNr);
m_labelResult.Text = String.Format("Maximum number was {0}, it appears {1} times.", iMaxNr, iCount);
}
void buttonCalculateAverage_Click(object sender, EventArgs e)
{
IEnumerable<int> numbers = m_lbNumbers.Items.Cast<int>();
double dAverage = numbers.Average();
m_labelResult.Text = String.Format("Average of all numbers is {0}", dAverage);
}
void buttonGetMaximumNumberOfSelectedNumbers_Click(object sender, EventArgs e)
{
if(m_lbNumbers.SelectedItems.Count > 0)
{
IEnumerable<int> numbers = m_lbNumbers.SelectedItems.Cast<int>();
int iMaxNr = numbers.Max();
m_labelResult.Text = String.Format("Maximum number of the selected numbers was {0}", iMaxNr);
}
else
{
m_labelResult.Text = "No numbers selected, select at least two numbers";
}
}
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
随意提出任何问题,我会尝试帮助 - 但正如我所说,如果你想通过软件开发成功,没有什么能阻止你学习所有的基础知识。
亲切的问候
Johannes
Feel free to ask any questions, I'll try to help - but as I said, nothing will prevent you from learning all the basics if you want to succed with software develpment.
Kind regards
Johannes
这篇关于我该如何解决两个问题呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!