Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using IWshRuntimeLibrary;
- namespace test
- {
- public partial class TestFileDelete : Form
- {
- Boolean flag = true;
- public TestFileDelete()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- // disables while running click code
- this.button1.Enabled = false;
- // This doesnt seem to do anything either?
- if (this.button1.Enabled == false) return;
- // just a either or flag to swtich the end of the shortcut names to see if they are deleted or not with one button
- int switchnum = (flag) ? 1 : 0;
- int newnum = (flag) ? 0 : 1;
- if (flag == true)
- {
- flag = false;
- }
- else
- {
- flag = true;
- }
- // this is a bit messy, its taken from my larger project. but this code shows my problem. (in fact my larger project only requires a 200ms timeout, this needs 300..?)
- String StartMenuProgName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu), "Programs", "skbt config", "config " + newnum);
- Dictionary<UInt32, String> Shortcuts = new Dictionary<uint, string>() {
- {0,Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Start Keepalive (" + "config " + newnum + ").lnk")},
- {1,Path.Combine(StartMenuProgName, "Start Keepalive.lnk")},
- {2,Path.Combine(StartMenuProgName, "Auto Restart Test.lnk")},
- {3,Path.Combine(StartMenuProgName, "Manual Restart.lnk")},
- {4,Path.Combine(StartMenuProgName, "Manual Start.lnk")},
- {5,Path.Combine(StartMenuProgName, "Manual Stop.lnk")},
- {6,Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "README.txt")}
- };
- foreach (KeyValuePair<UInt32, String> shortcut in Shortcuts)
- {
- while (System.IO.File.Exists(Path.GetFullPath(shortcut.Value.ToString())))
- {
- // Will only delete with a delay of at least 300 ms.????
- if (this.checkBox1.Checked == true)
- {
- Thread.Sleep(300);
- }
- System.IO.File.Delete(Path.GetFullPath(shortcut.Value.ToString()));
- }
- }
- if (Directory.Exists(StartMenuProgName)) { Directory.Delete(StartMenuProgName, true); }
- // Current Name
- StartMenuProgName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu), "Programs", "skbt config", "config " + switchnum);
- // Create Shortcuts on desktop / add to programs start menu
- String BatchLibPath = @"c:\test";
- if (!addShortcut(
- Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Start Keepalive (" + "config " + switchnum + ").lnk"),
- Path.Combine(BatchLibPath, "start_keepalive.bat")))
- {
- MessageBox.Show("There was an error creating a shortcut in the Start Menu (0).");
- }
- if (!addShortcut(
- Path.Combine(StartMenuProgName, "Start Keepalive.lnk"),
- Path.Combine(BatchLibPath, "start_keepalive.bat")))
- {
- MessageBox.Show("There was an error creating a shortcut in the Start Menu (1).");
- }
- if (!addShortcut(
- Path.Combine(StartMenuProgName, "Auto Restart Test.lnk"),
- Path.Combine(BatchLibPath, "control", "auto_restart.bat")))
- {
- MessageBox.Show("There was an error creating a shortcut in the Start Menu (2).");
- }
- if (!addShortcut(
- Path.Combine(StartMenuProgName, "Manual Restart.lnk"),
- Path.Combine(BatchLibPath, "control", "manual_restart.bat")))
- {
- MessageBox.Show("There was an error creating a shortcut in the Start Menu (3).");
- }
- if (!addShortcut(
- Path.Combine(StartMenuProgName, "Manual Start.lnk"),
- Path.Combine(BatchLibPath, "control", "manual_start.bat")))
- {
- MessageBox.Show("There was an error creating a shortcut in the Start Menu (4).");
- }
- if (!addShortcut(
- Path.Combine(StartMenuProgName, "Manual Stop.lnk"),
- Path.Combine(BatchLibPath, "control", "manual_stop.bat")))
- {
- MessageBox.Show("There was an error creating a shortcut in the Start Menu (5).");
- }
- this.button1.Enabled = true;
- }
- private static Boolean addShortcut(String ShortcutPath, String TargetPath)
- {
- try
- {
- WshShell shell = new WshShell();
- IWshShortcut link = (IWshShortcut)shell.CreateShortcut(ShortcutPath);
- link.TargetPath = TargetPath;
- if (!Directory.Exists(Path.GetDirectoryName(ShortcutPath)))
- {
- Directory.CreateDirectory(Path.GetDirectoryName(ShortcutPath));
- }
- link.Save();
- return true;
- }
- catch
- {
- return false;
- }
- }
- // Does Nothing when used in that loop.
- protected virtual bool IsFileLocked(FileInfo file)
- {
- FileStream stream = null;
- try
- {
- stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
- }
- catch (IOException)
- {
- //the file is unavailable because it is:
- //still being written to
- //or being processed by another thread
- //or does not exist (has already been processed)
- return true;
- }
- finally
- {
- if (stream != null)
- stream.Close();
- }
- //file is not locked
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement