Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Use shift key to perform default action, and ctrl key for default path #45

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
169eee6
Added ctrl and shift key combination logic
David-Maisonave Mar 2, 2022
c6197cb
Update frmMain.Designer.cs
David-Maisonave Mar 2, 2022
871507e
Update README.md
David-Maisonave Mar 2, 2022
3abfe4d
Update AssemblyInfo.cs
David-Maisonave Mar 2, 2022
f94ba8d
Merge branch 'master' of https://github.com/David-Maisonave/PasteInto…
David-Maisonave Mar 2, 2022
a89a50d
Update AssemblyInfo.cs
David-Maisonave Mar 2, 2022
eb8c67d
Update README.md
David-Maisonave Mar 2, 2022
4f71fca
Update README.md
David-Maisonave Mar 2, 2022
7c1a643
Update README.md
David-Maisonave Mar 2, 2022
b9becec
Update AssemblyInfo.cs
David-Maisonave Mar 2, 2022
6ea4b7c
Merge branch 'master' of https://github.com/David-Maisonave/PasteInto…
David-Maisonave Mar 2, 2022
4c1f0f2
Update AssemblyInfo.cs
David-Maisonave Mar 2, 2022
4816a13
Switch keys around (ctrl and shift)
David-Maisonave Mar 3, 2022
367c81d
Update frmMain.cs
David-Maisonave Mar 3, 2022
64ae4e9
Update README.md
David-Maisonave Mar 3, 2022
adf47e6
Update AssemblyInfo.cs
David-Maisonave Mar 4, 2022
4e2386a
Merge branch 'master' of https://github.com/David-Maisonave/PasteInto…
David-Maisonave Mar 4, 2022
982e50a
Added option to have fully qualified path for sub directories
David-Maisonave Mar 4, 2022
cb338e0
Update README.md
David-Maisonave Mar 4, 2022
0bc0835
Change command line option names
David-Maisonave Mar 4, 2022
425c228
Update README.md
David-Maisonave Mar 4, 2022
06c23ee
Update AssemblyInfo.cs
David-Maisonave Mar 4, 2022
a0868c9
Merge branch 'master' of https://github.com/David-Maisonave/PasteInto…
David-Maisonave Mar 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 54 additions & 62 deletions PasteIntoFile/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,50 @@ static void Main(string[] args)
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length>0)
{
if (args[0] == "/reg")
try
{
RegisterApp();
return;
if (string.Equals(args[0], "/reg", StringComparison.CurrentCultureIgnoreCase))
{
RegisterApp();
return;
}
else if (string.Equals(args[0], "/unreg", StringComparison.CurrentCultureIgnoreCase))
{
UnRegisterApp();
return;
}
else if (string.Equals(args[0], "/filename", StringComparison.CurrentCultureIgnoreCase) && args.Length > 1)
{
var key = OpenDirectoryKey().CreateSubKey("shell").CreateSubKey("Paste Into File");
key = key.CreateSubKey("filename");
key.SetValue("", args[1]);

MessageBox.Show("Filename has been registered with your system", "Paste Into File", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else if (string.Equals(args[0], "/TextDefaultDir", StringComparison.CurrentCultureIgnoreCase) && args.Length > 1)
{
var key = OpenDirectoryKey().CreateSubKey("shell").CreateSubKey("Paste Into File");
key = key.CreateSubKey("TextDefaultDir");
key.SetValue("", args[1]);

MessageBox.Show("TextDefaultDir has been registered with your system", "Paste Into File", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else if (string.Equals(args[0], "/ImageDefaultDir", StringComparison.CurrentCultureIgnoreCase) && args.Length > 1)
{
var key = OpenDirectoryKey().CreateSubKey("shell").CreateSubKey("Paste Into File");
key = key.CreateSubKey("ImageDefaultDir");
key.SetValue("", args[1]);

MessageBox.Show("ImageDefaultDir has been registered with your system", "Paste Into File", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
else if (args[0] == "/unreg")
catch (Exception ex)
{
UnRegisterApp();
return;
MessageBox.Show(ex.Message + "\nPlease run the application as Administrator !", "Paste As File", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (args[0] == "/filename")
{
if (args.Length > 1) {
RegisterFilename(args[1]);
}
return;
}
Application.Run(new frmMain(args[0]));
}
else
Expand All @@ -46,64 +73,29 @@ static void Main(string[] args)

}

public static void RegisterFilename(string filename)
{
try
{
var key = OpenDirectoryKey().CreateSubKey("shell").CreateSubKey("Paste Into File");
key = key.CreateSubKey("filename");
key.SetValue("", filename);

MessageBox.Show("Filename has been registered with your system", "Paste Into File", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
//throw;
MessageBox.Show(ex.Message + "\nPlease run the application as Administrator !", "Paste As File", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

public static void UnRegisterApp()
{
try
{
var key = OpenDirectoryKey().OpenSubKey(@"Background\shell", true);
key.DeleteSubKeyTree("Paste Into File");

key = OpenDirectoryKey().OpenSubKey("shell", true);
key.DeleteSubKeyTree("Paste Into File");
var key = OpenDirectoryKey().OpenSubKey(@"Background\shell", true);
key.DeleteSubKeyTree("Paste Into File");

MessageBox.Show("Application has been Unregistered from your system", "Paste Into File", MessageBoxButtons.OK, MessageBoxIcon.Information);
key = OpenDirectoryKey().OpenSubKey("shell", true);
key.DeleteSubKeyTree("Paste Into File");

}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\nPlease run the application as Administrator !", "Paste Into File", MessageBoxButtons.OK, MessageBoxIcon.Error);

}
MessageBox.Show("Application has been Unregistered from your system", "Paste Into File", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

public static void RegisterApp()
{
try
{
var key = OpenDirectoryKey().CreateSubKey(@"Background\shell").CreateSubKey("Paste Into File");
key.SetValue("Icon", "\"" + Application.ExecutablePath + "\",0");
key = key.CreateSubKey("command");
key.SetValue("" , "\"" + Application.ExecutablePath + "\" \"%V\"");

key = OpenDirectoryKey().CreateSubKey("shell").CreateSubKey("Paste Into File");
key.SetValue("Icon", "\"" + Application.ExecutablePath + "\",0");
key = key.CreateSubKey("command");
key.SetValue("" , "\"" + Application.ExecutablePath + "\" \"%1\"");
MessageBox.Show("Application has been registered with your system", "Paste Into File", MessageBoxButtons.OK, MessageBoxIcon.Information);
var key = OpenDirectoryKey().CreateSubKey(@"Background\shell").CreateSubKey("Paste Into File");
key.SetValue("Icon", "\"" + Application.ExecutablePath + "\",0");
key = key.CreateSubKey("command");
key.SetValue("", "\"" + Application.ExecutablePath + "\" \"%V\"");

}
catch (Exception ex)
{
//throw;
MessageBox.Show(ex.Message + "\nPlease run the application as Administrator !", "Paste As File", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
key = OpenDirectoryKey().CreateSubKey("shell").CreateSubKey("Paste Into File");
key.SetValue("Icon", "\"" + Application.ExecutablePath + "\",0");
key = key.CreateSubKey("command");
key.SetValue("", "\"" + Application.ExecutablePath + "\" \"%1\"");
MessageBox.Show("Application has been registered with your system", "Paste Into File", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

public static void RestartApp()
Expand Down
14 changes: 7 additions & 7 deletions PasteIntoFile/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("PasteIntoFile")]
[assembly: AssemblyTitle("Paste Clipboard Contents Into File")]
[assembly: AssemblyDescription("Paste Clipboard Contents Into Files")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("EslaMxSoft 2014")]
[assembly: AssemblyProduct("PasteIntoFile v1.4")]
[assembly: AssemblyCopyright("Copyright © EslaMxSoft 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCompany("EslaMxSoft 2022")]
[assembly: AssemblyProduct("PasteIntoFile")]
[assembly: AssemblyCopyright("Copyright © EslaMxSoft 2022")]
[assembly: AssemblyTrademark("EslaMxSoft 2022")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
Expand All @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.4.0.1")]
[assembly: AssemblyFileVersion("1.4.0.1")]
[assembly: AssemblyVersion("1.6.2.0")]
[assembly: AssemblyFileVersion("1.6.2.0")]
19 changes: 2 additions & 17 deletions PasteIntoFile/frmMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 82 additions & 22 deletions PasteIntoFile/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ namespace PasteAsFile
{
public partial class frmMain : Form
{
public const string DEFAULT_FILENAME_FORMAT = "yyyy-MM-dd HH.mm.ss";
public string CurrentLocation { get; set; }
public bool IsText { get; set; }
public frmMain()
public const string DEFAULT_FILENAME_FORMAT = "yyyy-MM-dd HH.mm.ss";
public string CurrentLocation { get; set; }
public bool IsText { get; set; }
public frmMain()
{
InitializeComponent();
}
Expand All @@ -31,11 +31,20 @@ public frmMain(string location)
}
private void frmMain_Load(object sender, EventArgs e)
{
string filename = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Classes\Directory\shell\Paste Into File\filename", "", null) ?? DEFAULT_FILENAME_FORMAT;
txtFilename.Text = DateTime.Now.ToString(filename);
txtCurrentLocation.Text = CurrentLocation ?? @"C:\";

if (Registry.GetValue(@"HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\Paste Into File\command", "", null) == null)
string filename = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Classes\Directory\shell\Paste Into File\filename", "", null) ?? DEFAULT_FILENAME_FORMAT;
txtFilename.Text = DateTime.Now.ToString(filename);
txtCurrentLocation.Text = CurrentLocation ?? @"C:\";
const string DEFAULT_TEXT_SUBFOLDER = "Text";
const string DEFAULT_IMAGE_SUBFOLDER = "Image";
string TextDefaultDir = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Classes\Directory\shell\Paste Into File\TextDefaultDir", "", null) ?? DEFAULT_TEXT_SUBFOLDER;
string ImageDefaultDir = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Classes\Directory\shell\Paste Into File\ImageDefaultDir", "", null) ?? DEFAULT_IMAGE_SUBFOLDER;
if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
{
string SubDir = (Clipboard.ContainsText()) ? TextDefaultDir : ImageDefaultDir;
txtCurrentLocation.Text = (SubDir.IndexOf(":") > 0) ? SubDir : txtCurrentLocation.Text + @"\" + SubDir;
}

if (Registry.GetValue(@"HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\Paste Into File\command", "", null) == null)
{
if (MessageBox.Show("Seems that you are running this application for the first time,\nDo you want to Register it with your system Context Menu ?", "Paste Into File", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Expand All @@ -46,30 +55,68 @@ private void frmMain_Load(object sender, EventArgs e)
if (Clipboard.ContainsText())
{
lblType.Text = "Text File";
comExt.Items.AddRange(new object[] {
"ahk", // - AutoHotkey
"au3", // - AutoIt
"bat", // - DOS Batch
"cmd", // - DOS Batch (modern)
"cpp", // - C++ Language
"cs", // - C-Sharp Langauge
"css", // - Cascading Style Sheets - use to style an HTML document
"csv", // - Comma-Separated Value
"htm", // - Hypertext Markup Language
"html", // - Hypertext Markup Language
"ini", // - INI - Windows Configuration File
"java", // - Java Language
"js", // - JavaScript Language
"json", // - JavaScript Object Notation
"php", // - PHP Language
"pl", // - Perl Language
"ps1", // - PowerShell
"py", // - Python Language
"reg", // - Registry File
"swift", // - Switft Language
"txt", // - Text Files
"vb", // - Visual Basic Language
"vbs", // - Visual Basic Script Language
"xml" // - Extensible Markup Language
});
comExt.SelectedItem = "txt";
IsText = true;
txtContent.Text = Clipboard.GetText();
return;
txtContent.Text = Clipboard.GetText();
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
Save_Action(0);
return;
}

if (Clipboard.ContainsImage())
{
lblType.Text = "Image";
comExt.Items.AddRange(new object[] {
"png",
"jpg",
"bmp",
"gif",
"ico"
});
comExt.SelectedItem = "png";
imgContent.Image = Clipboard.GetImage();
imgContent.Image = Clipboard.GetImage();
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
Save_Action(0);
return;
}

lblType.Text = "Unknown File";
btnSave.Enabled = false;

}

}

private void btnSave_Click(object sender, EventArgs e)
public void Save_Action(int millisecondsTimeout)
{
string location = txtCurrentLocation.Text;
location = location.EndsWith("\\") ? location : location + "\\";
if (!Directory.Exists(location))
Directory.CreateDirectory(location);
location = location.EndsWith("\\") ? location : location + "\\";
string filename = txtFilename.Text + "." + comExt.SelectedItem.ToString();
if (IsText)
{
Expand Down Expand Up @@ -106,10 +153,15 @@ private void btnSave_Click(object sender, EventArgs e)

Task.Factory.StartNew(() =>
{
Thread.Sleep(1000);
if (millisecondsTimeout > 0)
Thread.Sleep(millisecondsTimeout);
Environment.Exit(0);
});
}
private void btnSave_Click(object sender, EventArgs e)
{
Save_Action(1000);
}

private void btnBrowseForFolder_Click(object sender, EventArgs e)
{
Expand All @@ -133,11 +185,19 @@ private void lblMe_Click(object sender, EventArgs e)

private void lblHelp_Click(object sender, EventArgs e)
{
string msg = "Paste Into File helps you paste any text or images in your system clipboard into a file directly instead of creating new file yourself";
msg += "\n--------------------\nTo Register the application to your system Context Menu run the program as Administrator with this argument : /reg";
msg += "\nto Unregister the application use this argument : /unreg\n";
msg += "\nTo change the format of the default filename, use this argument: /filename yyyy-MM-dd_HHmm\n";
msg += "\n--------------------\nSend Feedback to : eslamx7@gmail.com\n\nThanks :)";
string msg = "Paste Into File helps you paste any text or images in your system clipboard into a file directly instead of creating new file yourself\n";
msg += "-----------------------------------------------------------------\n";
msg += "To Register the application to your system Context Menu run the program as Administrator with argument: /reg\n";
msg += "To Unregister the application use argument: /unreg\n";
msg += "To change the format of the default filename, use argument:\n/filename yyyy-MM-dd_HHmm\n";
msg += "-----------------------------------------------------------------\n";

msg += "To create a file automatically without a window prompt, hold shift key while selecting 'Paste Into File'\n";
msg += "To add a default sub folder to 'Current Location', hold ctrl key while selecting 'Paste Into File'. ";
msg += "The default sub folder for a text file is Text, and the default sub folder for an image file is Image.\n\n";
msg += "To change the default Text Sub Folder, use argument:\n/TextDefaultDir MyDefaultTextFolder\n";
msg += "To change the default Image Sub Folder, use argument:\n/ImageDefaultDir MyImgDir\n";
msg += "\n--------------------\nSend Feedback to : eslamx7@gmail.com\n\nThanks :)";
MessageBox.Show(msg, "Paste As File Help", MessageBoxButtons.OK, MessageBoxIcon.Information);


Expand Down
Loading