Skip to content

Commit fa7cb8c

Browse files
committed
Ask for elevation if unauthorized
1 parent d650daa commit fa7cb8c

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

PasteIntoFile/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,14 @@ public static void RegisterApp()
144144
}
145145
}
146146

147-
public static void RestartApp()
147+
public static void RestartAppElevated(string location)
148148
{
149149
ProcessStartInfo proc = new ProcessStartInfo();
150150
proc.UseShellExecute = true;
151151
proc.WorkingDirectory = Environment.CurrentDirectory;
152152
proc.FileName = Application.ExecutablePath;
153153
proc.Verb = "runas";
154+
proc.Arguments = "\"" + location + "\"";
154155

155156
try
156157
{

PasteIntoFile/Properties/Resources.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PasteIntoFile/Properties/Resources.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
<value>Explorer context menu entry has been removed</value>
150150
</data>
151151
<data name="str_message_run_as_admin" xml:space="preserve">
152-
<value>Please run the application as Administrator!</value>
152+
<value>Please run the application as administrator!</value>
153153
</data>
154154
<data name="str_message_register_context_menu_success" xml:space="preserve">
155155
<value>Explorer context menu entry has been added</value>

PasteIntoFile/frmMain.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,13 @@ private void frmMain_Load(object sender, EventArgs e)
156156
private void btnSave_Click(object sender, EventArgs e)
157157
{
158158
string location = txtCurrentLocation.Text;
159-
location = location.EndsWith("\\") ? location : location + "\\";
160-
string filename = txtFilename.Text + "." + comExt.SelectedItem;
159+
string file = location + (location.EndsWith("\\") ? "" : "\\");
160+
file += txtFilename.Text + (txtFilename.Text.EndsWith("." + comExt.SelectedItem) ? "" : "." + comExt.SelectedItem);
161161
try
162162
{
163163
if (IsText)
164164
{
165-
File.WriteAllText(location + filename, txtContent.Text, Encoding.UTF8);
165+
File.WriteAllText(file, txtContent.Text, Encoding.UTF8);
166166
}
167167
else
168168
{
@@ -179,7 +179,7 @@ private void btnSave_Click(object sender, EventArgs e)
179179
default: format = ImageFormat.Png; break;
180180
}
181181

182-
imgContent.BackgroundImage.Save(location + filename, format);
182+
imgContent.BackgroundImage.Save(file, format);
183183
}
184184

185185
if (clrClipboard.Checked)
@@ -200,6 +200,7 @@ private void btnSave_Click(object sender, EventArgs e)
200200
catch (UnauthorizedAccessException ex)
201201
{
202202
MessageBox.Show(ex.Message + "\n" + Resources.str_message_run_as_admin, Resources.str_main_window_title, MessageBoxButtons.OK, MessageBoxIcon.Error);
203+
Program.RestartAppElevated(location);
203204
}
204205
catch (Exception ex)
205206
{

0 commit comments

Comments
 (0)