PDA

View Full Version : دریافت آرگومان جدید (نام چند فایل) توسط یک نمونه از برنامه در حال اجرا؛ Single Instance App



mn_acer1
یک شنبه 30 خرداد 1395, 03:09 صبح
سلام
دوستان اینجا کسی با File Association آشنایی داره؟
مثلا یه کار کنی فایل های MP3 با برنامه خود آدم پخش بشه!

ramtinak
دوشنبه 31 خرداد 1395, 10:01 صبح
سلام،
این کلاس برای File Association هست:

public class FileAssociation
{
public static void Associate(string extension,
string progID, string description, string icon, string application)
{
Registry.ClassesRoot.CreateSubKey(extension).SetVa lue("", progID);
if (progID != null && progID.Length > 0)
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(progID))
{
if (description != null)
key.SetValue("", description);
if (icon != null)
key.CreateSubKey("DefaultIcon").SetValue("", ToShortPathName(icon));
if (application != null)
key.CreateSubKey(@"Shell\Open\Command").SetValue("",
ToShortPathName(application) + " \"%1\"");
}
}

public static bool IsAssociated(string extension)
{
return (Registry.ClassesRoot.OpenSubKey(extension, false) != null);
}

[DllImport("Kernel32.dll")]
private static extern uint GetShortPathName(string lpszLongPath, [Out] StringBuilder lpszShortPath, uint cchBuffer);

private static string ToShortPathName(string longName)
{
StringBuilder s = new StringBuilder(1000);
uint iSize = (uint)s.Capacity;
uint iRet = GetShortPathName(longName, s, iSize);
return s.ToString();
}
}


روش استفاده:
FileAssociation.Associate(".pashmakproj", "02639d71-0935-35e8-9d1b-9dd1a2a34627", "Pashmak Project",
@"C:\Users\Pashmak\Desktop\icon.ico",
@"C:\Users\Pashmak\Desktop\wppp.exe");

توضیحات بیشتر در سافت افزار

mn_acer1
دوشنبه 31 خرداد 1395, 17:41 عصر
سلام،
این کلاس برای File Association هست:

public class FileAssociation
{
public static void Associate(string extension,
string progID, string description, string icon, string application)
{
Registry.ClassesRoot.CreateSubKey(extension).SetVa lue("", progID);
if (progID != null && progID.Length > 0)
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(progID))
{
if (description != null)
key.SetValue("", description);
if (icon != null)
key.CreateSubKey("DefaultIcon").SetValue("", ToShortPathName(icon));
if (application != null)
key.CreateSubKey(@"Shell\Open\Command").SetValue("",
ToShortPathName(application) + " \"%1\"");
}
}

public static bool IsAssociated(string extension)
{
return (Registry.ClassesRoot.OpenSubKey(extension, false) != null);
}

[DllImport("Kernel32.dll")]
private static extern uint GetShortPathName(string lpszLongPath, [Out] StringBuilder lpszShortPath, uint cchBuffer);

private static string ToShortPathName(string longName)
{
StringBuilder s = new StringBuilder(1000);
uint iSize = (uint)s.Capacity;
uint iRet = GetShortPathName(longName, s, iSize);
return s.ToString();
}
}


روش استفاده:
FileAssociation.Associate(".pashmakproj", "02639d71-0935-35e8-9d1b-9dd1a2a34627", "Pashmak Project",
@"C:\Users\Pashmak\Desktop\icon.ico",
@"C:\Users\Pashmak\Desktop\wppp.exe");

توضیحات بیشتر در سافت افزار

مرسی داداش. ولی اینو خودم میدونم که چه جوری برناممو رو یه پسوندی ست کنم! کلا میخواستم بدون کی با این موضوع کار کرده و آشنایی داره.
سوال اصلیم اینه که فرص کنید 5 تا فایل آهنگ رو از تو محیط ویندوز سلکت میکنم بعد رو دکمه اینتر که میزنم میخوام اون فایل ها به پلی لیست موزیک پلیری که درست کردم اضافه بشه! در واقع میخوام بدونم چه جوری میتونم آدرس اون فایل ها رو پیدا کنم؟
چون وقتی تو رجیستری یه پسوندی رو ست میکنی فقط آدرس یک فایل رو به عنوان آرگومنت به برنامه میفرسته ولی من میخوام همه فایل هایی که انتخاب میشند فرستاده بشه آدرسشون.

Mahmoud.Afrad
سه شنبه 01 تیر 1395, 02:18 صبح
از لینک زیر استفاده کنید (رفرنس Microsoft.VisualBasic نیاز است)
http://www.hanselman.com/blog/TheWeeklySourceCode31SingleInstanceWinFormsAndMicr osoftVisualBasicdll.aspx
میتونید کد لینک بالا رو به صورتی اصلاح کنید که با یک رویداد بتونید آرگومانهای جدید رو دریافت کنید:
using System;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using Microsoft.VisualBasic.ApplicationServices;

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
string[] arguments = Environment.GetCommandLineArgs();
SingleInstanceController controller = new SingleInstanceController(new Form1());
controller.Run(arguments);
}

public class SingleInstanceController : WindowsFormsApplicationBase
{
private Form _frm;

internal delegate void RecieveDelegate(object sender, CommandLineEventArgs e);

public static event RecieveDelegate RecievedArgs;

public SingleInstanceController(Form form)
{
_frm = form;
IsSingleInstance = true;

StartupNextInstance += SingleInstanceController_StartupNextInstance;
}

private void SingleInstanceController_StartupNextInstance(objec t sender, StartupNextInstanceEventArgs e)
{
OnRecievedArgs(new CommandLineEventArgs(e.CommandLine));
}

protected override void OnCreateMainForm()
{
MainForm = _frm;
}

protected virtual void OnRecievedArgs(CommandLineEventArgs e)
{
if (RecievedArgs != null) RecievedArgs(this, e);
}

public class CommandLineEventArgs : EventArgs
{
private ReadOnlyCollection<string> _commandLineArguments;

public CommandLineEventArgs(ReadOnlyCollection<string> commandLineArguments)
{
_commandLineArguments = commandLineArguments;
}

public ReadOnlyCollection<string> CommandLineArguments
{
get { return _commandLineArguments; }
}
}
}
}
حالا میتونید در جایی که نیاز دارید رویداد RecievedArgs رو هندل و از طریق پراپرتی commandLineArguments مقادیر پارامترها رو بدست بیارید.

mn_acer1
سه شنبه 01 تیر 1395, 02:35 صبح
از لینک زیر استفاده کنید (رفرنس Microsoft.VisualBasic نیاز است)
http://www.hanselman.com/blog/TheWeeklySourceCode31SingleInstanceWinFormsAndMicr osoftVisualBasicdll.aspx
میتونید کد لینک بالا رو به صورتی اصلاح کنید که با یک رویداد بتونید آرگومانهای جدید رو دریافت کنید:
using System;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using Microsoft.VisualBasic.ApplicationServices;

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
string[] arguments = Environment.GetCommandLineArgs();
SingleInstanceController controller = new SingleInstanceController(new Form1());
controller.Run(arguments);
}

public class SingleInstanceController : WindowsFormsApplicationBase
{
private Form _frm;

internal delegate void RecieveDelegate(object sender, CommandLineEventArgs e);

public static event RecieveDelegate RecievedArgs;

public SingleInstanceController(Form form)
{
_frm = form;
IsSingleInstance = true;

StartupNextInstance += SingleInstanceController_StartupNextInstance;
}

private void SingleInstanceController_StartupNextInstance(objec t sender, StartupNextInstanceEventArgs e)
{
OnRecievedArgs(new CommandLineEventArgs(e.CommandLine));
}

protected override void OnCreateMainForm()
{
MainForm = _frm;
}

protected virtual void OnRecievedArgs(CommandLineEventArgs e)
{
if (RecievedArgs != null) RecievedArgs(this, e);
}

public class CommandLineEventArgs : EventArgs
{
private ReadOnlyCollection<string> _commandLineArguments;

public CommandLineEventArgs(ReadOnlyCollection<string> commandLineArguments)
{
_commandLineArguments = commandLineArguments;
}

public ReadOnlyCollection<string> CommandLineArguments
{
get { return _commandLineArguments; }
}
}
}
}
حالا میتونید در جایی که نیاز دارید رویداد RecievedArgs رو هندل و از طریق پراپرتی commandLineArguments مقادیر پارامترها رو بدست بیارید.

ممنون. ولی بعید میدونم که در این کد هم بیش از 1 آرگومان فرستاده بشه! میشه!؟
از همه این کدهایی که واسه Single Instance کردن برنامه هست استفاده کردم از این سینگل اینستنس ویبی هم استفاده کردم ولی همچنان فقط آدرس یک فایل فرستاده میشه! البته اینی که شما دادین رو امتحان نکردم ولی بیسش به هرحال باید همون مدلی باشه!