在列表框中选择的C#打开文件? [英] C# open file that is selected on listbox?

查看:97
本文介绍了在列表框中选择的C#打开文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个列表框,显示您计算机上的所有启动文件。无论如何,当我点击该项目并单击按钮禁用它实际上删除启动项目?我不认为这是以前被问过的,因此会受到整个社区的赞赏和... ... ME:



这是错误:不能找到文件:C:\ ... \bin\Debug \ steam''。

如何使用列表框上使用的信息?



到目前为止的代码...:





  private   void  DisableBtn_Click( object  sender,RoutedEventArgs e)
{
foreach string program in startupinfo.SelectedItems)
{
FileStream fileStream = new FileStream(program,FileMode.Open);
尝试
{
// 从文件读取或写入文件
}
最后
{
fileStream。关();
}

解决方案

我不知道如何与您交谈(请参阅对解决方案1的评论)。您可能需要知道涉及的目录是什么。请参阅我过去的全面答案:

如何找到我的程序目录可执行目录),

如何查找我的程序目录当前目录,特殊文件夹



-SA


看起来您正在尝试读取文件stream并假设此文件位于您的工作目录或可执行文件目录中。该异常只是告诉您找不到该文件。你需要确保文件在那里,你应该在阅读或写入之前检查它,否则你应该处理文件无法打开时抛出的异常。



当您对运行时行为稍有担忧时,应始终在调试器下执行应用程序。你应该在问这些问题之前先做这件事,因为它可以帮助你提供帮助你所需要的信息。



-SA

Hello I have a Listbox that shows up all startup files on your computer. Is there anyway that when I click on the item and click on button disable it actually removes the startup item? I don''t think this has been asked ever before and therefore would be appreciated for whole community and...ME :)

This is the error: Could not find file:C:\...\bin\Debug\steam''.
How can it use the information that was used that is on the Listbox?

Code so far...:


private void DisableBtn_Click(object sender, RoutedEventArgs e)
        {
foreach (string program in startupinfo.SelectedItems)
{
    FileStream fileStream = new FileStream(program, FileMode.Open);
    try
    {
        // read from file or write to file
    }
    finally
    {
        fileStream.Close();
    }

解决方案

I don''t know how to talk with you (please see the comments to Solution 1). You probably need to know what are the involved directories. Please see my past comprehensive answers:
How to find my programs directory ( executable directory),
How to find my programs directory (current directory, "special folders").

—SA


It looks like you are trying to read the file "stream" and assume that this file is in your working directory or in the executable directory. The exception simply tells you that the file not found. You need to make sure that the file is there, you you should check it up before reading or writing, otherwise yo should handle the exception thrown when the file fails to open.

You should always execute your application under the debugger when you have a slightest concern about your runtime behavior. And you should always do it before asking such questions as yours, as it can help you to provide information needed to help you.

—SA


这篇关于在列表框中选择的C#打开文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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