Advertisement
Guest User

C# File Delete Bug in .NET

a guest
Jun 9th, 2015
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using IWshRuntimeLibrary;
  13.  
  14. namespace test
  15. {
  16.     public partial class TestFileDelete : Form
  17.     {
  18.         Boolean flag = true;
  19.  
  20.         public TestFileDelete()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         private void button1_Click(object sender, EventArgs e)
  26.         {
  27.             // disables while running click code
  28.             this.button1.Enabled = false;
  29.  
  30.             // This doesnt seem to do anything either?
  31.             if (this.button1.Enabled == false) return;
  32.  
  33.             // just a either or flag to swtich the end of the shortcut names to see if they are deleted or not with one button
  34.             int switchnum = (flag) ? 1 : 0;
  35.             int newnum = (flag) ? 0 : 1;
  36.             if (flag == true)
  37.             {
  38.                 flag = false;
  39.             }
  40.             else
  41.             {
  42.                 flag = true;
  43.             }
  44.  
  45.             // 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..?)
  46.             String StartMenuProgName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu), "Programs", "skbt config", "config " + newnum);
  47.  
  48.             Dictionary<UInt32, String> Shortcuts = new Dictionary<uint, string>() {
  49.                         {0,Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Start Keepalive (" + "config " + newnum + ").lnk")},
  50.                         {1,Path.Combine(StartMenuProgName, "Start Keepalive.lnk")},
  51.                         {2,Path.Combine(StartMenuProgName, "Auto Restart Test.lnk")},
  52.                         {3,Path.Combine(StartMenuProgName, "Manual Restart.lnk")},
  53.                         {4,Path.Combine(StartMenuProgName, "Manual Start.lnk")},
  54.                         {5,Path.Combine(StartMenuProgName, "Manual Stop.lnk")},
  55.                         {6,Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "README.txt")}
  56.                     };
  57.             foreach (KeyValuePair<UInt32, String> shortcut in Shortcuts)
  58.             {
  59.                 while (System.IO.File.Exists(Path.GetFullPath(shortcut.Value.ToString())))
  60.                 {
  61.                     // Will only delete with a delay of at least 300 ms.????
  62.                     if (this.checkBox1.Checked == true)
  63.                     {
  64.                         Thread.Sleep(300);
  65.                     }
  66.                     System.IO.File.Delete(Path.GetFullPath(shortcut.Value.ToString()));
  67.                 }
  68.             }
  69.             if (Directory.Exists(StartMenuProgName)) { Directory.Delete(StartMenuProgName, true); }
  70.  
  71.  
  72.             // Current Name
  73.             StartMenuProgName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu), "Programs", "skbt config", "config " + switchnum);
  74.  
  75.             // Create Shortcuts on desktop / add to programs start menu
  76.             String BatchLibPath = @"c:\test";
  77.  
  78.             if (!addShortcut(
  79.                 Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Start Keepalive (" + "config " + switchnum + ").lnk"),
  80.                 Path.Combine(BatchLibPath, "start_keepalive.bat")))
  81.             {
  82.                 MessageBox.Show("There was an error creating a shortcut in the Start Menu (0).");
  83.             }
  84.  
  85.             if (!addShortcut(
  86.                 Path.Combine(StartMenuProgName, "Start Keepalive.lnk"),
  87.                 Path.Combine(BatchLibPath, "start_keepalive.bat")))
  88.             {
  89.                 MessageBox.Show("There was an error creating a shortcut in the Start Menu (1).");
  90.             }
  91.  
  92.             if (!addShortcut(
  93.                 Path.Combine(StartMenuProgName, "Auto Restart Test.lnk"),
  94.                 Path.Combine(BatchLibPath, "control", "auto_restart.bat")))
  95.             {
  96.                 MessageBox.Show("There was an error creating a shortcut in the Start Menu (2).");
  97.             }
  98.  
  99.             if (!addShortcut(
  100.                 Path.Combine(StartMenuProgName, "Manual Restart.lnk"),
  101.                 Path.Combine(BatchLibPath, "control", "manual_restart.bat")))
  102.             {
  103.                 MessageBox.Show("There was an error creating a shortcut in the Start Menu (3).");
  104.             }
  105.  
  106.             if (!addShortcut(
  107.                 Path.Combine(StartMenuProgName, "Manual Start.lnk"),
  108.                 Path.Combine(BatchLibPath, "control", "manual_start.bat")))
  109.             {
  110.                 MessageBox.Show("There was an error creating a shortcut in the Start Menu (4).");
  111.             }
  112.  
  113.             if (!addShortcut(
  114.                 Path.Combine(StartMenuProgName, "Manual Stop.lnk"),
  115.                 Path.Combine(BatchLibPath, "control", "manual_stop.bat")))
  116.             {
  117.                 MessageBox.Show("There was an error creating a shortcut in the Start Menu (5).");
  118.             }
  119.             this.button1.Enabled = true;
  120.         }
  121.  
  122.         private static Boolean addShortcut(String ShortcutPath, String TargetPath)
  123.         {
  124.             try
  125.             {
  126.                 WshShell shell = new WshShell();
  127.                 IWshShortcut link = (IWshShortcut)shell.CreateShortcut(ShortcutPath);
  128.                 link.TargetPath = TargetPath;
  129.  
  130.                 if (!Directory.Exists(Path.GetDirectoryName(ShortcutPath)))
  131.                 {
  132.                     Directory.CreateDirectory(Path.GetDirectoryName(ShortcutPath));
  133.                 }
  134.                 link.Save();
  135.  
  136.                 return true;
  137.             }
  138.             catch
  139.             {
  140.                 return false;
  141.             }
  142.         }
  143.  
  144.         // Does Nothing when used in that loop.
  145.         protected virtual bool IsFileLocked(FileInfo file)
  146.         {
  147.             FileStream stream = null;
  148.  
  149.             try
  150.             {
  151.                 stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
  152.             }
  153.             catch (IOException)
  154.             {
  155.                 //the file is unavailable because it is:
  156.                 //still being written to
  157.                 //or being processed by another thread
  158.                 //or does not exist (has already been processed)
  159.                 return true;
  160.             }
  161.             finally
  162.             {
  163.                 if (stream != null)
  164.                     stream.Close();
  165.             }
  166.  
  167.             //file is not locked
  168.             return false;
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement