Not sure why this moderator or admin or whatever deleted my previous thread because it wasn't anything malicious.. My problem is that getfileattribute doesn't work for me.. and my code below is just a snippet of my entire program which is below that..

Dear Mod: I think you deleted it because you saw me testing it on a crack file.. obviously im going to test it on any file I can find on my desktop other than a text file because it isn't working.. Next time wait until I wake up to defend myself before you infract me. The program is meant to delete viruses and folders containing them.. You enter the path to them in the textboxes and press delete.. thats not malicious. I meant to also put:

if(remove(File.c_str())  //Doesn't work on hidden or read only files..
{
   MessageBox::Show("Deleted successfully");
}
GET_FILEEX_INFO_LEVELS fInfoLevelId = GetFileExInfoStandard;
WIN32_FILE_ATTRIBUTE_DATA lpFileInformation;
int ret;
 
ret = GetFileAttributesEx((LPCWSTR)File.c_str(), fInfoLevelId, &lpFileInformation);

if(lpFileInformation.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
{
	MessageBox::Show("archive");
}
if(lpFileInformation.dwFileAttributes == FILE_ATTRIBUTE_COMPRESSED)
{
	MessageBox::Show("comp");
}
if(lpFileInformation.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
	MessageBox::Show("dir");
}
if(lpFileInformation.dwFileAttributes == FILE_ATTRIBUTE_READONLY)
{
	MessageBox::Show("readonly");
}
if(lpFileInformation.dwFileAttributes == FILE_ATTRIBUTE_HIDDEN)
{
	MessageBox::Show("hidden");
}

My whole Program...

#define REPEAT do{
#define UNTIL( condition ) }while(!(condition));

#pragma once
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "user32.lib")
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <sys/stat.h> 


namespace FileDeleter {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Security::Permissions;
	using namespace System::Runtime::InteropServices;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::TextBox^  textBox1;
	protected: 
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::Button^  button2;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(12, 36);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(241, 20);
			this->textBox1->TabIndex = 0;
			this->textBox1->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &Form1::textBox1_KeyPress);
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(13, 90);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(240, 20);
			this->textBox2->TabIndex = 1;
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(12, 20);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(76, 13);
			this->label1->TabIndex = 2;
			this->label1->Text = L"File To Delete:";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(16, 71);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(89, 13);
			this->label2->TabIndex = 3;
			this->label2->Text = L"Folder To Delete:";
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(268, 36);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(36, 23);
			this->button1->TabIndex = 4;
			this->button1->Text = L"Del";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(268, 87);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(36, 23);
			this->button2->TabIndex = 5;
			this->button2->Text = L"Del";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackgroundImageLayout = System::Windows::Forms::ImageLayout::None;
			this->ClientSize = System::Drawing::Size(316, 122);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->textBox1);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedToolWindow;
			this->MaximizeBox = false;
			this->MinimizeBox = false;
			this->Name = L"Form1";
			this->Text = L"File Shredder";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
			 }
	private: System::Void textBox1_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) {
			 }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				   std::string File;
				   String^ FileName = textBox1->Text;
				   MarshalString(FileName, File);
				   LPCWSTR FileCW;

				    LPCWSTR chars = (LPCWSTR)(Marshal::StringToHGlobalAnsi(FileName)).ToPointer();
					FileCW = chars;
					Marshal::FreeHGlobal(IntPtr((void*)chars));

					GET_FILEEX_INFO_LEVELS fInfoLevelId = GetFileExInfoStandard;
					WIN32_FILE_ATTRIBUTE_DATA lpFileInformation;
					int ret;
 
					ret = GetFileAttributesEx((LPCWSTR)File.c_str(), fInfoLevelId, &lpFileInformation);

					if(lpFileInformation.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
					{
						MessageBox::Show("archive");
					}
					if(lpFileInformation.dwFileAttributes == FILE_ATTRIBUTE_COMPRESSED)
					{
						MessageBox::Show("comp");
					}
					if(lpFileInformation.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
					{
						MessageBox::Show("dir");
					}
					if(lpFileInformation.dwFileAttributes == FILE_ATTRIBUTE_READONLY)
					{
						MessageBox::Show("readonly");
					}
					if(lpFileInformation.dwFileAttributes == FILE_ATTRIBUTE_HIDDEN)
					{
						MessageBox::Show("hidden");
					}
		 }
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
		 }
void MarshalString (String ^ s, std::string& os) {
		const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
		os = chars;
		Marshal::FreeHGlobal(IntPtr((void*)chars));
	}
};
}

Recommended Answers

All 2 Replies

> ret = GetFileAttributesEx((LPCWSTR)File.c_str(), fInfoLevelId, &lpFileInformation);
The first problem is you can't just go around casting whatever just to make the compiler STFU. If the function really is expecting a wide string, then simply casting a narrow string pointer just doesn't cut it.
You need a proper conversion of File.c_str() into a TCHAR string.
See http://msdn.microsoft.com/en-us/library/aa269775%28v=vs.60%29.aspx and onwards.

> if(lpFileInformation.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
The file attributes is a bit-mask containing many discrete bits (eg. read-only directory).
So you need to use bit masking to test each bit, like so.

if( (lpFileInformation.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) == FILE_ATTRIBUTE_ARCHIVE)

As for the remove problem, look up the relevant Win32 API for removing files.

Since you are using CLR/C++ (managed code) instead of just c++ you should be calling System.IO.FileSystemInfo's GetAttributes instead of the win32 api function GetFileAttributesEx().

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.