views:

84

answers:

1

I'm trying to duplicate Impero's policy lock screen.

I'm trying to have my program search to see if a file exists. The concept is that if the file exists, it will be launched.

What it be better to use a background worker to search for it? If so how would I make it search for it?

Any suggestions on how to implement the desired functionality?

A: 

Since you did not specify a language or a framework, I'll assume you're using C#. You said:

I'm trying to have my program search to see if a file exists. The concept is that if the file exists, it will be launched.

Here's a short snippet that will do what you've asked.

string path = @"C:\path\to\file";

if (File.Exists(path))
{
   Process p = new Process();
   p.StartInfo.FileName = path; // launches the default application for this file
   p.Start();
}

This is slightly simplistic: p.Start() could throw exceptions for a number of reasons. Additionally, you have little control over which application opens the file - it's entirely up to the user's registry. For example, if the user selects an HTML file to open, some users will see the file opened using Internet Explorer while other users will have Firefox installed, and so Firefox will be used to view the file.

Update:

Normally, I search for files using the following:

string[] matches = Directory.GetFiles(@"C:\", "*.txt");

Which returns all the paths to all the files on the C:\ drive ending with .txt. Of course, your search pattern will be different.

If the call to Directory.GetFiles( ) (or whatever you're using - you did not specify) takes a long time, then yes, using a BackgroundWorker is a good approach. Here's how you might do it. In the constructor of a class (you might choose to call it SearchFiles):

string searchPattern;
string searchDirectory;

BackgroundWorker worker;

string[] matches;

/// <summary>
/// Constructs a new SearchFiles object.
/// </summary>
public SearchFiles(string pattern, string directory)
{
         searchPattern = pattern;
         searchDirectory = directory;

         worker = new BackgroundWorker();
         worker.DoWork += FindFiles;
         worker.RunWorkerCompleted += FindFilesCompleted;

         worker.RunWorkerAsync();
}

void FindFilesCompleted(object sender, RunWorkerCompletedEventArgs e)
{
      if (e.Error == null)
      {
         // matches should contain all the 'found' files.

         // you should fire an event to notify the caller of the results
      }
}


void FindFiles(object sender, DoWorkEventArgs e)
{
      // this is the code that takes a long time to execute, so it's
      // in the DoWork event handler.
      matches = System.IO.Directory.GetFiles(searchDirectory,searchPattern);
}

I hope this answers your question!

Charlie Salts
So wait. that's not actually the full script?
tjk0102
and how would i have it search for the file in the background before actually opening the program?
tjk0102
I've updated my answer to give you some more information about searching for files. I see in one of your comments you said you're using "basic". Do you mean *Visual* Basic? The MSDN BackgroundWorker page: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx has examples in 3 languages; the syntax is different but the concepts are all the same.
Charlie Salts
i am using visual basic,when i put in the commands that you gave me string[] matches = Directory.GetFiles(@"C:\", "*.txt"); visual says that string cannot be used as an expression and directory is not declared.
tjk0102
but also reading back at my original question i found an error. the program is secretly running in the background and every like 5 seconds or so it check's drive c and checks a folder called "shutdown policy" and inside there is a text document called "commands" if the text says start..then background worker or something will activate form 1 which brings up a countdown screenif the text document say's stop then it aborts system shutdown..weird to ask but could you please create that for me then send me the the visual file (Save project and send it) if you say yes i'll give you my email..
tjk0102
It sounds like you're trying to circumvent some security system. You're on your own.
Charlie Salts
Great thanks any way
tjk0102