0

im kinda new to programming and As university project,i have to write a program which changes a file info like a virus and then undo the changes just like anti virus.

i wrote the code for changing attribute on read only,But what about Hidden or system file ? and what is the way for undoing it !

where im going wrong in coding ??

Here is my main form code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    OpenFileDialog fDialog;

    void button1_Click(object sender, EventArgs e) // Browse button
    {
        fDialog = new OpenFileDialog();
        fDialog.Title = "Open a Text File";
        fDialog.Filter = "TXT Files|*.txt|doc Files|*.doc";
        fDialog.InitialDirectory = @"C:\";
        if (fDialog.ShowDialog() == DialogResult.OK)
    {
     MessageBox.Show(fDialog.FileName.ToString());
     }
        textBox1.Text = fDialog.FileName;
        fDialog.AddExtension = true;
        fDialog.CheckFileExists = true;
        fDialog.CheckPathExists = true;
    }

    private void textBox1_TextChanged(object sender, EventArgs e)//the path showing text box
    {
    }

    private void button2_Click(object sender, EventArgs e)//read-only button
    {
        fDialog.ReadOnlyChecked = true;
    }

    private void button3_Click(object sender, EventArgs e) //Hidden button
    {

    }
    }
}
MehraD
  • 429
  • 4
  • 16
  • possible duplicate of [C# make file read/write from readonly](http://stackoverflow.com/questions/8081242/c-sharp-make-file-read-write-from-readonly) – Sriram Sakthivel Mar 30 '14 at 07:42
  • Its not,i didnt used attributes code !- its funny but it seems i dont have such code in my visual,or i did some thing really wrong !! – MehraD Mar 30 '14 at 07:45
  • but that's what you're looking for. refer to joe's answer he shows how to set readonly and revert it – Sriram Sakthivel Mar 30 '14 at 07:46
  • As i said i cant use attribute codes (set attribute,get attribute),i dont know why ?? did i write something wrong ? |||||||| what about hidden option,what should i do ? – MehraD Mar 30 '14 at 07:49
  • for hidden you've to use `FileAttributes.Hidden` instead of `FileAttributes.ReadOnly`. and what do you meant by i cant use attribute codes. Why? post your attempt showing whats wrong. Any error? – Sriram Sakthivel Mar 30 '14 at 07:53
  • this is what i get when i use attribute code :> Error 2 'System.Windows.Forms.OpenFileDialog' does not contain a definition for 'GetAttributes' and no extension method 'GetAttributes' accepting a first argument of type 'System.Windows.Forms.OpenFileDialog' could be found (are you missing a using directive or an assembly reference?) c:\users\mehrad\documents\visual studio 2012\Projects\WindowsFormsApplication6\WindowsFormsApplication6\Form1.cs 50 – MehraD Mar 30 '14 at 07:56
  • Did you refer the answer I suggested that says `File.GetAttributes` not `OpenFileDialog.GetAttributes` – Sriram Sakthivel Mar 30 '14 at 07:59
  • OMG !! now i see what i missed,i used wrong DT for this code ! shall i delete this question ? – MehraD Mar 30 '14 at 08:01
  • Sure, if you're convinced with that answer :) – Sriram Sakthivel Mar 30 '14 at 08:05
  • @SriramSakthivel i also have another question: how can i use file path and file name in other buttons to set other attributions ? – MehraD Mar 30 '14 at 08:30
  • Once the user selects the file you gonna save selected file path in a instance variable(string), then use it in all other buttons. Am I missing something? – Sriram Sakthivel Mar 30 '14 at 08:34
  • yeah,it was correct ! and also what kind of variables should i use for "File" to do this code: > File.GetAttributes("THE STRING VARIABLE HERE"); – MehraD Mar 30 '14 at 08:46
  • `File.GetAttributes(fDialog.FileName)` you can store it in a instance member and use it. In short it should be the file full path – Sriram Sakthivel Mar 30 '14 at 09:05

0 Answers0