首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

=========怎样选中文件夹中的某个文件?========解决思路

2012-02-13 
怎样选中文件夹中的某个文件???例如目录c:\123\456\下有个文件是1.txt我用Process.Start

=========怎样选中文件夹中的某个文件???========
例如目录   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.
//


int count = 20;
if (count + startingIndex > = files.Length)
{
count = files.Length - startingIndex;
}

// Begin the cross-thread call. Because you are passing
// immutable objects into this invoke method, you do not have to
// wait for it to finish. If these were complex objects, you would
// have to either create new instances of them or
// wait for the thread to process this invoke before modifying
// the objects.
IAsyncResult r = BeginInvoke(fileListDelegate, new object[] {files, startingIndex, count});
startingIndex += count;
}

// Now that you have finished the files in this directory, recurse for
// each subdirectory.
string[] directories = Directory.GetDirectories(directory);
foreach(string d in directories)
{
RecurseDirectory(Path.Combine(d, search));
}
}

/// <summary>
/// This is the actual thread procedure. This method runs in a background
/// thread to scan directories. When finished, it simply exits.
/// </summary>
private void ThreadProcedure()
{
// Get the search string. Individual
// field assigns are atomic in .NET, so you do not
// need to use any thread synchronization to grab
// the string value here.
try
{
string localSearch = SearchCriteria;

// Now, search the file system.
//
RecurseDirectory(localSearch);
}
finally
{
// You are done with the search, so update.
//
searching = false;

// Raise an event that notifies the user that
// the search has terminated.
// You do not have to do this through a marshaled call, but
// marshaling is recommended for the following reason:
// Users of this control do not know that it is
// multithreaded, so they expect its events to
// come back on the same thread as the control.
BeginInvoke(onSearchComplete, new object[] {this, EventArgs.Empty});
}
}
}
}

[解决办法]
偏方来了
Explorer.exe /select,c:\boot.ini
有个API,看名称是很正点,可是不会用SHOpenFolderAndSelectItems

热点排行