日期:2014-05-20  浏览次数:21281 次

=========怎样选中文件夹中的某个文件???========
例如目录   c:\123\456\   下有个文件是1.txt
我用   Process.Start(@ "C:\123\456 ");   可以打开这个文件夹,现在我想在打开文件夹的同时选中   1.txt,如果文件夹中的文件很多,还要确保这个文件可见(即让其显示在当前屏幕里,不需要再拖动滚动条),该如何实现?

------解决方案--------------------

protected override void OnHandleDestroyed(EventArgs e)
{
// If the handle is being destroyed and you are not
// recreating it, then abort the search.
if (!RecreatingHandle)
{
StopSearch();
}
base.OnHandleDestroyed(e);
}

protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if (deferSearch)
{
deferSearch = false;
BeginSearch();
}
}

/// <summary>
/// This method is called by the background thread when it has finished
/// the search.
/// </summary>
/// <param name= "sender "> </param>
/// <param name= "e "> </param>
private void OnSearchComplete(object sender, EventArgs e)
{
if (SearchComplete != null)
{
SearchComplete(sender, e);
}
}

public void StopSearch()
{
if (!searching)
{
return;
}

if (searchThread.IsAlive)
{
searchThread.Abort();
searchThread.Join();
}

searchThread = null;
searching = false;
}

/// <summary>
/// Recurses the given path, adding all files on that path to
/// the list box. After it finishes with the files, it
/// calls itself once for each directory on the path.
/// </summary>
/// <param name= "searchPath "> </param>
private void RecurseDirectory(string searchPath)
{
// Split searchPath into a directory and a wildcard specification.
//
string directory = Path.GetDirectoryName(searchPath);
string search = Path.GetFileName(searchPath);

// If a directory or search criteria are not specified, then return.
//
if (directory == null || search == null)
{
return;
}

string[] files;

// File systems like NTFS that have
// access permissions might result in exceptions
// when looking into directories without permission.
// Catch those exceptions and return.
try
{
files = Directory.GetFiles(directory, search);
}
catch(UnauthorizedAccessException)
{
return;
}
catch(DirectoryNotFoundException)
{
return;
}

// Perform a BeginInvoke call to the list box
// in order to marshal to the correct thread. It is not
// very efficient to perform this marshal once for every
// file, so batch up multiple file calls into one
// marshal invocation.
int startingIndex = 0;

while(startingIndex < files.Length)
{
// Batch up 20 files at once, unless at the
// end.
//