I am trying to delete a file when my program draws itself (Using Visual Studio 2008).

Currently using this method

public frmMainMenu()
        {
            InitializeComponent();
			File.Delete("\\Program Files\\smartdeviceproject1\\SendXML\\1.txt");
        }

The path for the file is "\Program Files\smartdeviceproject1\SendXML\1.txt"

Yet when the program is executed, breakpointing shows that the line is executed (and no errors are thrwon) but the file remains in the directory

Screen shot - http://imgur.com/F6GOu.jpg

Is it possible that this is not working because I am using the compact framwework?

Dani AI

Generated

Quick practical follow-up and a short checklist — Compact Framework itself is rarely the culprit. Common causes are an incorrect path/filename, hidden file extensions, the file being locked by another process, or permission/UAC restrictions on the folder. was right to point out CF usually isn’t the issue; and correctly nudged toward absolute paths; and ’s “hide known file extensions” tip is exactly the kind of thing that produces a surprise filename like 1.txt.txt.

Verify what the runtime actually sees by listing the directory before deleting. For example:

string folder = @"C:\Program Files\smartdeviceproject1\SendXML";
foreach (var f in Directory.GetFiles(folder))
    System.Diagnostics.Debug.WriteLine(f);

That prints the exact filenames (and reveals double extensions or typos) so you don’t rely on what Explorer shows with extensions hidden.

Use existence checks and exception logging around the delete so failures are visible:

if (File.Exists(path)) {
    try {
        File.Delete(path);
    } catch (Exception ex) {
        System.Diagnostics.Debug.WriteLine("Delete failed: " + ex.Message);
    }
} else {
    System.Diagnostics.Debug.WriteLine("File not found: " + path);
}

If the file exists but won’t delete, check: (1) the file is not open by your process or another process, (2) folder permissions/UAC on desktop (Program Files often blocks writes), and (3) file attributes (hidden/system).

Final tips: enable visible file extensions in Explorer (Folder Options → View → uncheck “Hide extensions for known file types”), avoid using Program Files for runtime data (use an app data folder instead), and when working with a device/emulator, inspect the device filesystem with the emulator’s file explorer or remote file manager so you’re looking at the same filesystem your app is.

Recommended Answers

All 5 Replies

Might be ur application path and deleting file path is differnt.So specify the full path to delete the file like "c:\program files\....."

The sample code you have written is working. once try again. If you don't specify the drive in the path, it will choose the drive in which OS is installed.

>Is it possible that this is not working because I am using the compact framwework?

That is not problem with CF. File.Delete() will not throws an exception event if the specified file does not exist.

Thanks guys it was the path, stupidly I allowed my workmate to name the file for me and he obviously didn't name it 1.txt

He named it 1.txt.txt

aah, the age old problem of "Hide known file extensions" :p
remember to mark the thread as solved since you have found a solution

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.