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.
Related
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();
}
I created a button that will allow a user to create a CSV based on a directory comparison. I compiled the code, and everything looks ok. I go to run the program, however, and I get a "Access is denied" error.
private void button1_Click_1(object sender, EventArgs e)
{
string csv = string.Empty;
FolderBrowserDialog fbd = new FolderBrowserDialog();
using (var sfd = new SaveFileDialog())
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string folderPath = fbd.SelectedPath;
string filename = "DIR_" + (DateTime.Now.ToShortDateString()) + ".csv";
File.WriteAllText(folderPath + "DIR_" + (DateTime.Now.ToShortDateString()) + ".csv", csv);
}
Is there another way to save the datagridview into a CSV with the file extension give in the filename string?
Do you have permission to write on the folder?
Try use File.WriteAllLines:
File.WriteAllLines(folderPath + "DIR_" + (DateTime.Now.ToShortDateString()) + ".csv", csv);
screenshot
So this is a method that gets the directory of a file (is a .JDCEDFile but its is just renamed .zip file)
With this method i try to rename het file and extract it to a specified folder.
And show its contents into the right textboxes.
This method fails at Extracting process and i don't understand why.
public void OpenEncodedFile(string path)
{
// Variabelen + verwerking
string defaultpath = Application.StartupPath + #"\temp";
string defaultzip = path;
string defaultzipRename = defaultzip.Replace(".JDCEDFile", ".zip");
File.Move(defaultzip, defaultzipRename);
ZipFile.ExtractToDirectory(defaultzipRename, defaultpath);
Input.Text = File.ReadAllText(defaultpath + #"\tempData.txt");
Password.Text = File.ReadAllText(defaultpath + #"\tempPass.txt");
File.Move(defaultzipRename, defaultzip);
}
"path" has to be wrong, I just tested it and it worked fine.
private void openEncodedFileToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openEncodedFileDialog.ShowDialog() == DialogResult.OK)
{
OpenEncodedFile(openEncodedFileDialog.FileName);
}
}
This the the code.
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 have this backup and restore code in C# using MS Access as its database. I finished doing the backup in zip format and now I want to restore the Zipped file. Any help will be much appreciated.
public void BackupDatabase(string dateToday)
{
string dbFileName = "dbCPS.accdb";
string CurrentDatabasePath = Path.Combine(Environment.CurrentDirectory , dbFileName);
string backTimeStamp = Path.GetFileNameWithoutExtension(dbFileName) + "_" + dateToday + ".zip";// +Path.GetExtension(dbFileName);
string destFileName = backTimeStamp;// +dbFileName;
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
string PathtobackUp = fbd.SelectedPath.ToString();
destFileName = Path.Combine(PathtobackUp, destFileName);
//File.Copy(CurrentDatabasePath, destFileName, true);
using (var zip = new ZipFile())
{
zip.AddFile(dbFileName);
zip.Save(destFileName);
}
MessageBox.Show("Backup successful! ");
}
}
private void backupToolStripMenuItem1_Click(object sender, EventArgs e)
{
BackupDatabase(DateTime.Now.ToString("ddMMMyyyy_HH.mm"));
}
public void RestoreDatabase(string restoreFile)
{
string dbFileName = "dbCPS.accdb";
string pathBackup = restoreFile;
string CurrentDatabasePath = Path.Combine(Environment.CurrentDirectory, dbFileName);
File.Copy(pathBackup, CurrentDatabasePath, true);
MessageBox.Show("Restore successful! ");
}
private void restoreToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
openFileDialogBackUp.FileName = "dbCPS";
openFileDialogBackUp.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory + #"Sauvegardes";
if (openFileDialogBackUp.ShowDialog() == DialogResult.OK)
RestoreDatabase(openFileDialogBackUp.FileName);
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
}
This code extracts the zipped file but I dont know how to do the restore at the same time.
using (ZipFile zip = ZipFile.Read(restoreFile))
{
zip.ExtractAll(#"C:\Users\Desktop\backup");
}
According to the MSDN documentation here, you should be able to extract the zip file directly to the destination folder. Your existing "restore" code simply does a file copy to CurrentDatabasePath, so one would expect that a statement like...
System.IO.Compression.ZipFile.ExtractToDirectory(pathToZipFile, CurrentDatabasePath);
...would be all it takes. (I would test it myself, but ZipFile was apparently added in .NET 4.5 and my copy of Visual Studio is too old.)