Click here to Skip to main content
15,919,613 members
Home / Discussions / C#
   

C#

 
GeneralRe: BinaryFormatter - Serialize OutOfMemory Exception Pin
Eddy Vluggen23-Jun-15 5:57
professionalEddy Vluggen23-Jun-15 5:57 
QuestionSMTP - Failure sending mail. Pin
Hrishikesh Shivacharan10-Jun-15 23:12
Hrishikesh Shivacharan10-Jun-15 23:12 
AnswerRe: SMTP - Failure sending mail. Pin
OriginalGriff10-Jun-15 23:49
mveOriginalGriff10-Jun-15 23:49 
SuggestionRe: SMTP - Failure sending mail. Pin
Richard MacCutchan11-Jun-15 1:01
mveRichard MacCutchan11-Jun-15 1:01 
AnswerRe: SMTP - Failure sending mail. Pin
Abhipal Singh11-Jun-15 6:23
professionalAbhipal Singh11-Jun-15 6:23 
QuestionHow to write java script for image upload on choose file type not in click function Pin
balajiparamkusam10-Jun-15 4:42
balajiparamkusam10-Jun-15 4:42 
AnswerRe: How to write java script for image upload on choose file type not in click function Pin
CHill6010-Jun-15 4:56
mveCHill6010-Jun-15 4:56 
Question(SOLVED) C# .NET File.Delete() Not Working. Pin
uniflare9-Jun-15 16:28
uniflare9-Jun-15 16:28 
SOLUTION
make sure to refresh the folder your checking to see if the files have actually deleted or not.

In this case they were (the code works fine), just i wasn't refreshing desktop etc haha, oh well.

STUPID QUESTION CONTINUES-------------
http://s000.tinyupload.com/index.php?file_id=06162948769787766673[^]

The link above is a localized example of my problem.

I cannot for the life of me figure out why file.exists or any kind of permission checks don't work!

Its like .NET just says oh.. I might delete this, might not, might say i did but actually didn't, or didn't but actually.. didn't, maybe i will if you give me time... but i still wont tell you even if i did or didn't... Confused | :confused: Mad | :mad: Confused | :confused:

so... please save the last few strands of hair on my head! D'Oh! | :doh:

Thanks!

(For ease of viewing: pastebin)

or right here:
C#
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;
        }
    }
}


modified 9-Jun-15 23:59pm.

Questionhow to fill textbox with data from access database in c sharp windows application Pin
Being The Sharan Kumar9-Jun-15 7:31
Being The Sharan Kumar9-Jun-15 7:31 
AnswerRe: how to fill textbox with data from access database in c sharp windows application Pin
Richard Deeming9-Jun-15 7:58
mveRichard Deeming9-Jun-15 7:58 
QuestionSystem.Linq.Dynamic.DynamicExpression.ParseLambda issue with Select() command Pin
HUONG Minh-Luong9-Jun-15 4:31
HUONG Minh-Luong9-Jun-15 4:31 
QuestionRe: System.Linq.Dynamic.DynamicExpression.ParseLambda issue with Select() command Pin
Richard Deeming9-Jun-15 5:57
mveRichard Deeming9-Jun-15 5:57 
AnswerRe: System.Linq.Dynamic.DynamicExpression.ParseLambda issue with Select() command Pin
HUONG Minh-Luong9-Jun-15 7:14
HUONG Minh-Luong9-Jun-15 7:14 
QuestionHow to populate a DropDownList in a DataGrid? Pin
Member 85894998-Jun-15 3:51
Member 85894998-Jun-15 3:51 
AnswerRe: How to populate a DropDownList in a DataGrid? Pin
User 418025410-Jul-15 11:55
User 418025410-Jul-15 11:55 
Questionneed help to use telegram api in c# Pin
adalat1237-Jun-15 19:30
adalat1237-Jun-15 19:30 
AnswerRe: need help to use telegram api in c# Pin
Richard MacCutchan7-Jun-15 21:20
mveRichard MacCutchan7-Jun-15 21:20 
QuestionWhat do these c# lines mean???? Pin
Django_Untaken7-Jun-15 5:33
Django_Untaken7-Jun-15 5:33 
AnswerRe: What do these c# lines mean???? Pin
Dave Kreskowiak7-Jun-15 5:42
mveDave Kreskowiak7-Jun-15 5:42 
GeneralRe: What do these c# lines mean???? Pin
OriginalGriff7-Jun-15 6:04
mveOriginalGriff7-Jun-15 6:04 
GeneralRe: What do these c# lines mean???? Pin
Dave Kreskowiak7-Jun-15 6:35
mveDave Kreskowiak7-Jun-15 6:35 
GeneralRe: What do these c# lines mean???? Pin
OriginalGriff7-Jun-15 6:43
mveOriginalGriff7-Jun-15 6:43 
AnswerRe: What do these c# lines mean???? Pin
OriginalGriff7-Jun-15 6:04
mveOriginalGriff7-Jun-15 6:04 
AnswerRe: What do these c# lines mean???? Pin
Ravi Bhavnani9-Jun-15 6:34
professionalRavi Bhavnani9-Jun-15 6:34 
AnswerRe: What do these c# lines mean???? Pin
Gergilcan13-Jun-15 22:01
professionalGergilcan13-Jun-15 22:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.