Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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...:


C#
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();
    }
Posted
Comments
Sergey Alexandrovich Kryukov 10-Apr-13 18:08pm    
This is not really related to ListBox, but, if you ask a question on UI, you should specify full type name of ListBox. Or do you think there is only one? :-)
Now, this is not error, this is the exception. Always provide full exception information...
Why you are not using StreamReader or BinaryStreamReader (depending on what's in the file and how you want to read it)?
—SA
klottim 10-Apr-13 18:15pm    
As it is said in description it lists all startup items in your computer. On my computer: steam is installed.
I want to disable steam when it is selected and clicked on a button.
That's when the exception throws in.
That is the full exception, just removed some of the target area.
Sergey Alexandrovich Kryukov 10-Apr-13 18:19pm    
No, please don't tell something which is not true: this is not full exception information, not even close. Want to argue about it? Or want to know what's missing?

The name of the file "steam" is unusual. Are you sure this is what it should be? Are you sure it really exists?
Everything else is irrelevant.

Anyway, I answered.
—SA
klottim 10-Apr-13 18:23pm    
oh by the way I think I am close to a solution.
How do I make string "startfolder " read from string "program" ?

Code :
foreach (string program in startupinfo.SelectedItems)
{
string startfolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
Sergey Alexandrovich Kryukov 10-Apr-13 18:31pm    
Why would you need this folder? It has nothing to do with your application. Your exception message suggests that currently trying to use either executable path or working directory (which most often the same, but can be anything). What is the value of "program", does it contains any folder name? Do you want it? Which folder?

Do you need help to calculate executable directory?
—SA

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
 
Share this answer
 
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
 
Share this answer
 
Comments
klottim 10-Apr-13 18:20pm    
@Sergey Alexandrovich Kryukov
That is the problem im trying to fix.
Find the application path.

The listbox has the application path (obviously) else it wouldn't show up all my startup items.

However when I want to delete a selected item it don't use the information that listbox has.
Sergey Alexandrovich Kryukov 10-Apr-13 18:27pm    
What is to find here?! Run it under the debugger. Before opening the file, check up the file exists. It does not.
I'm not sure you have application path. What is the value of "program"? "stream", I guess? Then the behavior will depend on your working directory, which is, unlike assembly location, can be anything. It depends on how the user executes the code, not the code itself. Did you know that? But even this is irrelevant at the moment. You expect the file to exist, and it does not exist, what could be simpler?
Maybe you put wrong files when you populate the list view, but it does not matter. First figure out where the file is expected and where it actually is, if any. Then find out why it's wrong.

You provide not enough information to go further to help you and probably you don't need help. Just find out it all step by step, why asking?
—SA
Maciej Los 10-Apr-13 18:31pm    
If you can load file names to ListBox, then you should know how read their names from that ListBox.
There are many properties of ListBox and Item to store path to file.
Sergey Alexandrovich Kryukov 10-Apr-13 18:40pm    
This is not how it is done. One should create a class with full file name, and with the text to be shown in UI. This text should be returned in overridden ToString(). The objects of such class/struct should be added to ListView, instead of just strings. Do you see the point? This is what UI shows.
—SA
Maciej Los 10-Apr-13 18:27pm    
Simple and clever. My 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900