dungnk77
asked on
C#: Disallow multi-instances and display the BrowseForFolder dialog
In C#:
_ How to disable multi-instances of an C# application at the same time?
_ How to display the BrowseForFolder? and how to explore the specified folder at start of displaying.
_ How to disable multi-instances of an C# application at the same time?
_ How to display the BrowseForFolder? and how to explore the specified folder at start of displaying.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<excerpt from
http://search.experts-exchange.com/questions/20614186/prevent-second-app-instance-when-in-c.html >
Mutex (from System.Threading) can be used to simply stop the the new instance from running.
If you also want to bring the FIRST instance to the foreground, you also need FindWindow and SetForegroundWindow, from user32.dll.
Here's a sample that does these.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Runtime.InteropServ
namespace OnlyOneInstance
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
[DllImport("User32",EntryP
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("User32",EntryP
private static extern bool SetForegroundWindow(IntPtr
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Cont
this.Size = new System.Drawing.Size(300,30
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Mutex mutex = new Mutex(false, "ONLY LET SINGLE INSTANCE RUN");
bool Running = !mutex.WaitOne(0, false);
if (Running)
{
bool ret;
IntPtr hWnd = FindWindow(null, "Form1");
if(hWnd != IntPtr.Zero)
ret = SetForegroundWindow(hWnd);
}
else
Application.Run(new Form1());
}
}
}
You are looking for the xxxFileDialog class.
<exerpt from MSDN>
FileDialog is an abstract class, and cannot be instantiated directly. Additionally, you cannot inherit from this class. To create a dialog to select or save a file use OpenFileDialog or SaveFileDialog.
protected void button1_Click(object sender, System.EventArgs e)
{
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDir
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterInde
openFileDialog1.RestoreDir
if(openFileDialog1.ShowDia
{
if((myStream = openFileDialog1.OpenFile()
{
// Insert code to read the stream here.
myStream.Close();
}
}
}
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button in the upper-left corner of the page.
Here is another website on using mutext to disable multiple application instances
http://www.developerfusion.com/show/1716/7/
You should be able to do away with the DLL imports and just use the built in functions for a form.
(from Desp code)
...
if (AllProcesses.Length)>1) // if the process is running
{
this.Activate();
this.BringToFront();
}
else
{
Application.Run(new Form1());
}
...
On Windows systems the activate will make it flash if it is in the taskbar area.
(from Desp code)
...
if (AllProcesses.Length)>1) // if the process is running
{
this.Activate();
this.BringToFront();
}
else
{
Application.Run(new Form1());
}
...
On Windows systems the activate will make it flash if it is in the taskbar area.
ASKER
Thank you much for all your helps.
_ #1 - to disable multi-instances: it works very well.
_ #2 - to display BrowseForFolder: I mean that I want to display the dialog box allowing the user to select a folder. I have just found the way to do that. But there is still small problem: how to display the treeview and go straight to the specified folder (e.g. "C:\My folder")?
The following code is what I use:
public class BrowseForFolder : FolderNameEditor
{
#region Enumerations
public enum BrowserStylesEnum : int
{
BrowseForComputer = 0x00001000,
BrowseForEverything = 0x00004000,
BrowseForPrinter = 0x00002000,
RestrictToDomain = 0x00000002,
RestrictToFilesystem = 0x00000001,
RestrictToSubfolders = 0x00000008,
ShowTextBox = 0x00000010,
}
public enum FoldersEnum
{
Desktop = 0x00000000,
Favorites = 0x00000006,
MyComputer = 0x00000011,
MyDocuments = 0x00000005,
MyPictures = 0x00000027,
NetAndDialUpConnections = 0x00000031,
NetworkNeighborhood = 0x00000012,
Printers = 0x00000004,
Recent = 0x00000008,
SendTo = 0x00000009,
StartMenu = 0x0000000B,
Templates = 0x00000015
}
#endregion Enumerations
private FolderNameEditor.FolderBro wser m_oFolderBrowser = new FolderBrowser();
#region Properties, methods
public string Description
{
get
{
return m_oFolderBrowser.Descripti on;
}
set
{
m_oFolderBrowser.Descripti on = value;
}
}
public string DirectoryPath
{
get
{
return m_oFolderBrowser.Directory Path;
}
}
public FoldersEnum StartLocation
{
get
{
return (FoldersEnum)(int)m_oFolde rBrowser.S tartLocati on;
}
set
{
m_oFolderBrowser.StartLoca tion = (FolderBrowserFolder)(int) value;
}
}
public BrowserStylesEnum Style
{
get
{
return (BrowserStylesEnum)(int)m_ oFolderBro wser.Style ;
}
set
{
m_oFolderBrowser.Style = (FolderBrowserStyles)(int) value;
}
}
public DialogResult ShowDialog()
{
return m_oFolderBrowser.ShowDialo g();
}
public DialogResult ShowDialog(IWin32Window frmOwner)
{
return m_oFolderBrowser.ShowDialo g(frmOwner );
}
public BrowseForFolder()
{
}
~BrowseForFolder()
{
m_oFolderBrowser.Dispose() ;
}
#endregion Properties, methods
}
public static string BrowseFolders(BrowseForFol der.Browse rStylesEnu m nStyle, BrowseForFolder.FoldersEnu m nStartLocation, string strPrompt)
{
BrowseForFolder oFolderDialog = new BrowseForFolder();
if (nStyle != 0)
{
oFolderDialog.Style = nStyle;
}
// Havenot found out the way to go straight to the specified folder!!
oFolderDialog.StartLocatio n = nStartLocation;
oFolderDialog.Description = strPrompt;
oFolderDialog.ShowDialog() ;
return oFolderDialog.DirectoryPat h;
}
_ #1 - to disable multi-instances: it works very well.
_ #2 - to display BrowseForFolder: I mean that I want to display the dialog box allowing the user to select a folder. I have just found the way to do that. But there is still small problem: how to display the treeview and go straight to the specified folder (e.g. "C:\My folder")?
The following code is what I use:
public class BrowseForFolder : FolderNameEditor
{
#region Enumerations
public enum BrowserStylesEnum : int
{
BrowseForComputer = 0x00001000,
BrowseForEverything = 0x00004000,
BrowseForPrinter = 0x00002000,
RestrictToDomain = 0x00000002,
RestrictToFilesystem = 0x00000001,
RestrictToSubfolders = 0x00000008,
ShowTextBox = 0x00000010,
}
public enum FoldersEnum
{
Desktop = 0x00000000,
Favorites = 0x00000006,
MyComputer = 0x00000011,
MyDocuments = 0x00000005,
MyPictures = 0x00000027,
NetAndDialUpConnections = 0x00000031,
NetworkNeighborhood = 0x00000012,
Printers = 0x00000004,
Recent = 0x00000008,
SendTo = 0x00000009,
StartMenu = 0x0000000B,
Templates = 0x00000015
}
#endregion Enumerations
private FolderNameEditor.FolderBro
#region Properties, methods
public string Description
{
get
{
return m_oFolderBrowser.Descripti
}
set
{
m_oFolderBrowser.Descripti
}
}
public string DirectoryPath
{
get
{
return m_oFolderBrowser.Directory
}
}
public FoldersEnum StartLocation
{
get
{
return (FoldersEnum)(int)m_oFolde
}
set
{
m_oFolderBrowser.StartLoca
}
}
public BrowserStylesEnum Style
{
get
{
return (BrowserStylesEnum)(int)m_
}
set
{
m_oFolderBrowser.Style = (FolderBrowserStyles)(int)
}
}
public DialogResult ShowDialog()
{
return m_oFolderBrowser.ShowDialo
}
public DialogResult ShowDialog(IWin32Window frmOwner)
{
return m_oFolderBrowser.ShowDialo
}
public BrowseForFolder()
{
}
~BrowseForFolder()
{
m_oFolderBrowser.Dispose()
}
#endregion Properties, methods
}
public static string BrowseFolders(BrowseForFol
{
BrowseForFolder oFolderDialog = new BrowseForFolder();
if (nStyle != 0)
{
oFolderDialog.Style = nStyle;
}
// Havenot found out the way to go straight to the specified folder!!
oFolderDialog.StartLocatio
oFolderDialog.Description = strPrompt;
oFolderDialog.ShowDialog()
return oFolderDialog.DirectoryPat
}
** note..
using System.Diagnostics;