0

I am trying to write a line of code that will delete a specific file, but I have tried the DeleteFile function, and it is throwing an "Identifier not found" (Visual Studio error code C3861) message.

The code, which is inside of a button click event is:

DeleteFile(path+"filemaker\\start.ini");

What do I need in my form1.h to make this work?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Well, where did you declare `path`...? – Lightness Races in Orbit Dec 09 '14 at 17:43
  • path was declared within the button click event, before the DeleteFile function as: path=this->FilePathBox->Text; – TheMohawkNinja Dec 09 '14 at 17:47
  • I was actually having the `DeleteFile()` in mind. Is it declared? – sashoalm Dec 09 '14 at 17:48
  • @sashoalm From what I've seen, DeleteFile() doesn't seem to need to be declared, as all of the code examples for it don't include a declaration. – TheMohawkNinja Dec 09 '14 at 17:50
  • Please stop mixing C++ and C++/CLI as long as you´ve no idea of the difference. Or better, stop using C++/CLI completely until you know why you´re using it. And yes, in C++, you can´t "just use" DeleteFile without including something, and in .NET, you don´t need a native DeleteFile. – deviantfan Dec 09 '14 at 17:50
  • @deviantfan While I am not sure exactly which one I am using, I am well aware of why I am using it, as I am the most versed in it of all the languages that I know, and I know that the code is capable of doing the tasks that I am attempting to write (that is, deleting and writing files). – TheMohawkNinja Dec 09 '14 at 17:54
  • You don´t what language you´re using, but you know that this is the right command? Sorry, but that´s funny. Why not add a bit Java too? Recommendation: Stop writing code now, and spent some weeks reading about native binaries, what .NET and C# is, MS´ unlucky try to mix both worlds, the marketing decision to call it C++ in VS altough it´s not, "marshalling", char* vs System::String, charsets, etc.etc. – deviantfan Dec 09 '14 at 17:57
  • @deviantfan Or you could actually try and help me with the program and answer my question. I have no reason to learn what those things are at this moment in time. I may get to it at a later date, but for right now, the highest priority is why I can't seem to prograatically delete a file. – TheMohawkNinja Dec 09 '14 at 18:04
  • @TheMohawkNinja I did actually answer it (partially): You need the concept of marshalling and windows.h (former already written above, latter 10sec google if someone don´t know it). But reading about marshalling probably won´t help you much if you don´t understand the background, why you need it at all. And it´s not the only pitfall – deviantfan Dec 09 '14 at 18:06
  • @deviantfan, I don't know why you're outbursting at TheMohawkNinja. He's probably not using C++/CLI (I see no `^` nor `gcnew`s in his code) and is probably just using the windows api (i.e. `include "windows.h"` ). – g19fanatic Dec 09 '14 at 18:07
  • @g19fanatic While it´s no proof, earlier questions of him had the same problem, that he thought it was C++ when it wasn´t (eg. the one about the random thing). And I´m not outbursting, I´m perfectly calm. – deviantfan Dec 09 '14 at 18:11

1 Answers1

3

In manner to use DeleteFile you must #include <Windows.h> since it Win API function.

The argument must be a char* pointer, std::string can't be used as argument.

so you can do as follows:

std::string path = "\\path\\to\\dir\\";
std::string filename = path + "filemaker\\start.ini"; (when path does end with "\\")
DWORD res = DeleteFile(filename.c_str());

You can as well #include <stdio.h> (or <cstdio>) and use

int remove(const char* filename),

it is better since it is cross platform (ANSI C).

like this:

std::string path = "\\path\\to\\dir\\";
std::string filename = path + "filemaker\\start.ini"; (when path does end with "\\")
int res = remove(filename.c_str());

EDIT You need also to add marshaling, like this:

//includes
#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>

now the code:

String^ filepath=path+"filemaker\\start.ini";
const char* tmpptr= msclr::interop::marshal_as<const char*>(filepath);
DeleteFile(tmpptr);
SHR
  • 7,940
  • 9
  • 38
  • 57
  • 1
    Aside: There's no reason to use backslashes in paths on Windows. Only the command processor requires that. Then there's no ugly doubling. – Steve Fallows Dec 09 '14 at 18:25
  • The right thing to do is actually to get the path separator from the OS, since there are some languages, (like Korean and Japanese) which Windows don't use the "\\" nor "/" as the path separator. – SHR Dec 09 '14 at 18:50
  • Well, I am trying to do the DeleteFile option, but I am getting a C2228 error (left of '.c_str' must have class/structure/union). The code looks like: String^ filepath=path+"filemaker\\start.ini"; const char filechar = filepath.c_str(); DeleteFile(filechar); I placed the #include line in right under '#pragma once'. – TheMohawkNinja Dec 09 '14 at 19:28
  • See the edit at the buttom – SHR Dec 09 '14 at 19:48
  • Okay, I've had to do additional research, since that edit threw some other errors, for example, since it's a string to char, it needs to be marshal_context, and not marshal_as. It's not throwing any more errors, so what doesn't work now is just stuff that I'm going to have to figure out on my own. – TheMohawkNinja Dec 09 '14 at 23:35
  • Try to use `DeleteFileA`, or change the `char` to `TCHAR` – SHR Dec 09 '14 at 23:35
  • @TheMohawkNinja And if you finally understood that my comments above actually make sense, you can rethink about using .NET to delete the file. No need for marshalling and native methods, as said before. – deviantfan Dec 10 '14 at 01:35
  • It´s a one-liner, like http://msdn.microsoft.com/de-de/library/system.io.file.delete%28v=vs.110%29.aspx – deviantfan Dec 10 '14 at 01:42