Change file attribute pragmatically - c#

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
{
}
}
}

Related

Writing to CSV File in C#

I am creating a clock for clocking into a business. Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
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;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
String Code;
String Name;
String InOut;
Boolean Luke = true;
String csvPath = "C:/users/luke/documents/C#/csvProject.csv";
StringBuilder Header = new StringBuilder();
StringBuilder csvData = new StringBuilder();
public Form1()
{
InitializeComponent();
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
TopMost = true;
Header.AppendLine("Timestamp, Name");
File.AppendAllText(csvPath, Header.ToString());
textBox1.Font = new Font("Arial", 30, FontStyle.Bold);
}
private void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
Code = Code + button.Text;
textBox1.Text = Code;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
FormBorderStyle = FormBorderStyle.Sizable;
WindowState = FormWindowState.Normal;
TopMost = false;
}
}
private void button13_Click(object sender, EventArgs e)
{
//clear
Code = null;
textBox1.Text = Code;
}
private void button10_Click(object sender, EventArgs e)
{
//in or out
DateTime timeStamp = DateTime.Now;
if (Code == "123")
{
Name = "Luke";
}
Button button = (Button)sender;
csvData.AppendLine(timeStamp + "," + Name + "," + button.Text);
File.AppendAllText(csvPath, csvData.ToString());
Code = null;
textBox1.Text = Code;
}
private void button14_Click(object sender, EventArgs e)
{
}
}
}
My layout consists of a number pad, in button, and out button. When the user presses the in button after they enter their code, the program should write in the CSV file: Timestamp, Name, In. When I tested the code by clocking in, the program writes one row correctly. When I clock in and then clock out, it creates two rows of me clocking in and one row of me clocking out. I was wondering if anyone could help me find what is going wrong in the code. Thanks.
You need to empty csvData after writing it to the file.

How To Save Background Image On Form Close In C#

I am making a desktop app in c# in which when the user selects specific name of background from the menu strip, then the background shall turn to that required background. The problem is that i cannot save the user input, i tried settings but i cannot find "system.drawing.image" in settings so is there any way i can save the user changed background ? No external backgrounds a user is allowed to change, just the ones in the resource folder. Here is my code which shows error that system.drawing.color cannot take place of drawing.image.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TAC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
panel1.Location = new Point(165, 157);
panel2.Location = new Point(289, 158);
panel3.Location = new Point(47, 275);
panel4.Location = new Point(47, 402);
this.BackgroundImage = Properties.Settings.Default.FormImage;
}
private void bLUEToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = TAC.Properties.Resources.tex1;
}
private void gREENToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = TAC.Properties.Resources.tex2;
}
private void oRANGEToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = TAC.Properties.Resources.tex3;
}
private void rEDToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = TAC.Properties.Resources.tex4;
}
private void pURPLEToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = TAC.Properties.Resources.tex5;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.FormImage = this.BackgroundImage;
}
}
}
Use Save method to save settings
Properties.Settings.Default.Save();
If you want add an image to settings :
Add new setting with type string and use like this:
Save an image to setting ( when you close form)
MemoryStream ms = new MemoryStream();
Propertis.Resources.MyImage.Save(ms,ImageFormat.Jpeg);
Properties.Settings.Default.BackImg = Convert.ToBase64String(ms.ToArray());
Properties.Settings.Default.Save();
And read image from setting and set to background(in form load)
string img = Properties.Settings.Default.BackImg ;
byte[] i = Convert.FromBase64String(img);
this.BackgroundImage = Image.FromStream(new MemoryStream(i));
How add custom settings ?
http://www.codeproject.com/Articles/29130/Windows-Forms-Creating-and-Persisting-Custom-User

Load textBox Form title on load

I want to show current form Title inside the textBox
But the code below doesnt do it,it only works if I set it on Button1_click.
And After clicking the button it will change to the form title
But I need it to set the form title in the textbox instantly on load
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;
using System.Diagnostics;
namespace ShowTitle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Process currentp = Process.GetCurrentProcess();
textBox1.Text = currentp.MainWindowTitle;
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
This simple code demonstrate that when your event handler for Form.Load event is called there is no MainWindowTitle to read from the currentp, while if you execute the same code in the Form.Shown event handler there is a MainWindowTitle in the currentp variable
Form f;
TextBox t;
void Main()
{
f = new Form();
f.Text = "This is a test";
t = new TextBox();
f.Controls.Add(t);
f.Load += onLoad;
f.Shown += onShow;
f.Show();
}
void onLoad(object sender, EventArgs e)
{
Process currentp = Process.GetCurrentProcess();
if(!string.IsNullOrWhiteSpace(currentp.MainWindowTitle))
t.Text = currentp.MainWindowTitle;
else
t.Text = "NO TITLE";
}
void onShow(object sender, EventArgs e)
{
// Uncomment these line to see the differences
// if(!string.IsNullOrWhiteSpace(currentp.MainWindowTitle))
// t.Text = currentp.MainWindowTitle;
// else
// t.Text = "NO TITLE";
}

C# program keep on running despite no error

Am working in a project where I should give a crystal reports RPT file as input and get the properties of that report.
I am facing a problem in loading the report itself so I have used isLoaded() function to check whether the report is loaded or not.
I have used the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace sampleretreival
{
public partial class Form1 : Form
{
public string flName;
public Form1()
{
InitializeComponent();
Retrieve.Enabled = false;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstFiles.SelectedItems.Count > 0)
{
Retrieve.Enabled = true;
}
else
{
Retrieve.Enabled = false;
}
}
private void Browse_Click(object sender, EventArgs e)
{
openFileDialog1 = new OpenFileDialog();
openFileDialog1.Multiselect = false;
openFileDialog1.Filter = "Crystal Report Files | *.rpt";
openFileDialog1.ShowDialog();
flName = openFileDialog1.FileName;
if (flName.Length != 0)
{
lstFiles.Items.Insert(0, flName);
Retrieve.Enabled = true;
}
else
{
MessageBox.Show("Please select the crystal report files for analysis.", "SAMPLE", MessageBoxButtons.OK, MessageBoxIcon.Information);
Browse.Focus();
}
}
private void Retrieve_Click(object sender, EventArgs e)
{
int a=1;
ReportDocument rpt = new ReportDocument();
rpt.Load(flName);
int count = 5;
if (rpt.IsLoaded)
{
MessageBox.Show(count.ToString());
}
else
{
MessageBox.Show(a.ToString());
}
}
}
}
After compiling, I clicked the Browse button to select the report from the disk but when I click Retrieve button, the program keeps on running. I am not getting any output or any error.

how to read file from open file dialog and add to URL to play using wmplib?

I am making a simple music player implementing the WMPlib to play media files ....
I am trying to open the file using a open file dialog ... the dialog comes and able to select the file but an exception comes when I try to assign the filename to Player.URL
at the line
Player.URL = openFileDialog1.FileName;
the error says
Object reference not set to an instance of an object.
Can anyone please give me a clue on how to assign the filename to the player.URL
the complete code is as follows....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
WMPLib.WindowsMediaPlayer Player;
public Form1()
{
InitializeComponent();
}
private void PlayFile(String url)
{
Player = new WMPLib.WindowsMediaPlayer();
Player.PlayStateChange +=
new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange);
Player.MediaError +=
new WMPLib._WMPOCXEvents_MediaErrorEventHandler(Player_MediaError);
Player.URL = url;
Player.controls.play();
}
private void Player_PlayStateChange(int NewState)
{
if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
{
this.Close();
}
}
private void Player_MediaError(object pMediaObject)
{
MessageBox.Show("Cannot play media file.");
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "(mp3,wav,mp4,mov,wmv,mpg)|*.mp3;*.wav;*.mp4;*.mov;*.wmv;*.mpg|all files|*.*";
openFileDialog1.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
PlayFile(Player.URL);
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
MessageBox.Show(openFileDialog1.FileName);
Player.URL = openFileDialog1.FileName;
}
}
}
Make sure that you create an instance of WMPLib.WindowsMediaPlayer before using it. Right now it seems that you are clicking 'open file' button and trying to assign returned file name to null object.
Try to use openFileDialog1 like this:
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
Player.URL = openFileDialog1.FileName;
}
in button1_Click()

Categories

Resources