I have issue with this code Access denied
Code is working even when i check the text file i have text but i have Access denied Error
I tried to add App Manifest and have administrator rights even i try to set security on C drive for full access for my account
private void button1_Click(object sender, EventArgs e)
{
string dir = #"C:\Knjigovodstvo\Firme\"+textBox1.Text+"";
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
File.WriteAllText(Path.Combine(dir, "Podaci.txt"), textBox1.Text);
StreamWriter sw = new StreamWriter(dir);
sw.WriteLine("" + textBox1.Text + "");
sw.Close();
}
Just trying to not having the error because it is working
Thank you guys found the solution with #AlessandroD'Andria and #Steve help
private void button1_Click(object sender, EventArgs e)
{
string dir = #"C:\Knjigovodstvo\Firme\"+textBox1.Text+"";
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
File.WriteAllText(Path.Combine(dir, "Podaci.txt"), textBox1.Text);
StreamWriter sw = new StreamWriter("Podaci.txt");
sw.WriteLine("" + textBox1.Text + "");
sw.Close();
}
Related
I have a c# script in VS 2019 and now I want to write values to a File but where do Ihave to put the .txt file? After that I want to make a Setup.exe with Inno Setup Compiler.
So my question is where do I put the file to get access to it during programming my Programm and after I installed my .exe after I installed it from the Setup.exe.
I heard something about this but I dont know what this does:
string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "combinations.txt");
Now i set the Permission on top of the function as it was mention here but i still get the problem
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
private void button4_Click(object sender, EventArgs e)
{
string date = DateTime.Now.ToString("g");
string exePath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
string logFile = "combinations.txt";
if (File.Exists(Path.Combine(exePath, logFile)) == false)
File.Create(Path.Combine(exePath, logFile));
using (StreamWriter wr = File.AppendText(Path.Combine(exePath, logFile)))
{
wr.WriteLine(date + "|" + date);
wr.Close();
}
}
Thanks for your help
i think you can use this path correctly
it's in the folder where your exe works.
for example a writing log file in the exe folder.
in debug mode its in the debug folder.
public static void LogWrite(string mes)
{
string exepath= Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
DateTime _dt = DateTime.Now;
string logFile= "Log.Inc";
if (File.Exists(Path.Combine(exepath, logFile)) == false)
File.Create(Path.Combine(exepath, logFile));
using (StreamWriter wr = File.AppendText(Path.Combine(exepath, logFile)))
{
wr.WriteLine(_dt.ToString("dd/MM/yyyy") + "|" + _dt.ToString("hh:mm") + "|" + mes);
wr.Close();
}
}
Based on my test, you can try try the following code to log the information to the txt file when you have published it.
private void button1_Click(object sender, EventArgs e)
{
string date = DateTime.Now.ToString("g");
string exePath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
string logFile = "combinations.txt";
using (StreamWriter wr = new StreamWriter(Path.Combine(exePath, logFile), true))
{
wr.WriteLine(date + "|" + date);
wr.Close();
}
string path = Path.Combine(exePath, logFile);
MessageBox.Show(path);
}
If you use the above code, there is no need to check if the file is created.
Also, I show the path of the txt file in the final code.
i'm stuck with my program in c#. So the user has to press a button to create a random string (working fine) he can then chose to click on the other button. this one opens a filedialog and let him chose a dll file he want to rename into the random string. i can't get it working. it says my dll is already running in another process (wich is not). Any help is greatly appreciated :)
private void btnEncrypt_Click_1(object sender, EventArgs e)
{
// sets a random string to txtEncrypt.text
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog MyOpenFileDialog = new OpenFileDialog();
//filedialog
MyOpenFileDialog.Filter = "dll files (*.dll) |*.dll";//filter
MyOpenFileDialog.Title = "Chose the dll file";
MyOpenFileDialog.InitialDirectory = "C:\\Users\\Gebruiker\\Desktop";
MyOpenFileDialog.FilterIndex = 1;
MyOpenFileDialog.RestoreDirectory = true;
//if ok
if (MyOpenFileDialog.ShowDialog() == DialogResult.OK)
{
strPath = MyOpenFileDialog.FileName;
StreamReader reader = new StreamReader(strPath);
System.IO.File.Move(strPath, "C:\\Users\\Gebruiker\\Desktop\\" + txtEncrypt.Text + ".dll");
}
else //cancel
{
strPath = null;
}
It's because your StreamReader is opening the file so it can't be moved. That line doesn't appear to be doing anything anyway so you can probably remove it. Ideally replace it with
if (System.IO.File.Exists(strPath))
{
System.IO.File.Move(strPath, "C:\\Users\\Gebruiker\\Desktop\\" + txtEncrypt.Text + ".dll");
}
Just comment the initialization of the streem reader line or move it next to the rename line but don't forget to pass the new name
hi i have a webform where i am trying to create a txt file in a particular directory but i want to the name of the txt file to be entered by the user but I am not getting it how to do it please help
the code below creates a text file with name NameBox.Text.ToString I dont want that please help and thanks.
protected void Page_Load(object sender, EventArgs e)
{
NameLabel.Visible = false;
NameBox.Visible = false;
submit.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
NameLabel.Visible = true;
NameBox.Visible = true;
submit.Visible = true;
}
protected void submit_Click(object sender, EventArgs e)
{
// string fname = NameBox.Text;
string path = #"D:\NameBox.Text.ToString.txt";
try
{
// Delete the file if it exists.
if (File.Exists(path))
{
// Note that no lock is put on the
// file and the possibility exists
// that another process could do
// something with it between
// the calls to Exists and Delete.
File.Delete(path);
}
// Create the file.
using (FileStream fs = File.Create(path))
{
Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
// Open the stream and read it back.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString());
}
}
}
First, keep in mind that your IIS will not allow you to access your local file System as you expcect in a "console" application, maybe running with local admin rights.
Second, be sware of illegal characters for file names, you need to check or replace it.
And third, what if two users decide to use the same filename?
As Dejan Dakic already mentioned, step back and reconsider, maybe about using a database, like SQLite or something else.
string path = #"D:\NameBox.Text.ToString.txt";
That won't work. if your textfield is called "NameBox" you need to access the value of its text which is not possible within a String.
Try this:
String path = #"D:\" + NameBox.Text + ".txt";
If you're new to programming, I'd suggest to go get a book or some tutorials and get started that way! :)
I'm new to C#, I want to create a file with extension .txt and file name as with first three characters of textbox value.I am creating the file but i dont now how to store the file in the required destination for example c:\ Documents\ION.please help me.
Thank You For reading this...
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "Create_File";
string fileName = textBox1.Text.Substring(0, 3) + ".txt";
File.Create(fileName);
MessageBox.Show("ok");
}
Thank U guys ,with all Your help I solved it
private void button2_Click(object sender, EventArgs e)
{
button2.Text = "SAVE";
var files = Directory.GetFiles(#"C:\\Users\\Apple\\Desktop\\proj").Length;
string fileName = textBox1.Text.Substring(0, 3) + -+ ++files + ".txt";
string path2 = Path.GetFullPath("C:\\Users\\Apple\\Desktop\\proj");
string docPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string var = Path.Combine(docPath, path2);
string var1 = Path.Combine(var, fileName);
using (StreamWriter writer = new StreamWriter(var1))
{
string var6 = textBox1.Text;
string var7 = " ";
string var8 = textBox2.Text;
string var9 = string.Concat(var6, var7, var8);
writer.WriteLine(var9);
}
MessageBox.Show("File created");
}
string fileName = textBox.Text.Substring(0,3) + ".txt";
using(StreamWriter writer = new StreamWriter(fileName))
{
writer.WriteLine("Hello world!");
}
Note that it would be wise to confirm that the TextBox contains at least three characters before doing that.
This is the same issue i had and got it solved by doing it this way :
File.WriteAllText(folderPath + "DataExport_"+srchCat_txtbox.Text+"_"+DateTime.Now.ToString("ddMMyyyy")+".csv", csv);
I needed to save CSV files with a unique name which includes a global name "DataExport", a category name "srchCat_txtbox.Text" value and the current date.
I create an empty file and try to copy the contents from an existing to the newly created file. When i do that i am getting an IOexception
private void button1_Click(object sender, EventArgs e)
{
String test = textBox1.Text.ToString();
if (string.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("Enter the filename");
}
else
{
StreamWriter File = new StreamWriter(test);
MessageBox.Show(test + " Has been created");
}
}
private void button2_Click(object sender, EventArgs e)
{
String test = textBox1.Text.ToString();
try
{
File.Copy(#"D:\\Study this.txt", test);
}
catch (IOException)
{
MessageBox.Show("IO error occured");
}
}
You will need to close the Stream so in the else statement add File.Close(); that will release the newly created file. That section of the code will now look like this:
StreamWriter File = new StreamWriter(test);
File.Close();
MessageBox.Show(test + " Has been created");
replace line StreamWriter File = new StreamWriter(test); with below
using (File.Create(test)) ;
or with
using (StreamWriter writer = new StreamWriter(test)){}
reason for above change is you need to propery close the opend stream object before the copy.
using block will handle that for you.