I have to write an application, which will compare the modification date of two files. These files are Excel workbooks. The first file is located on a local drive and the second on a LAN network.
Any hints, how to write this app? There's no need to open these files, just to check the date from file attributes.
System.IO.FileInfo file1 = new System.IO.FileInfo(file1Name);
System.IO.FileInfo file2 = new System.IO.FileInfo(file2Name);
if(file1.LastWriteTime != file2.LastWriteTime)
//Do some stuff.
System.IO Namespace - FileInfo Class
Related
We get an excel file in a specific location where the name of the file may vary every time.
So every time we rename the excel file manually to "Report.xlsx" and then we have some operation to be done using a script task. The file_Path + file_Name is hardcoded in the script task as
Microsoft.Office.Interop.Excel.Workbook workBook = excelApp.Workbooks.Open(#"D:\Desktop\Report.xlsx");
Is there a way to get the file name from the file location and pass it to the above code ?
Note:
The file Path is static but the file name will vary every time.
At a time there will be only one excel file in the file Path.
We are already using a Sequence Container so cant change it.
Thanks in advance..
This will let you use the actual file name. Make sure to reference System.Linq
var dir = new System.IO.DirectoryInfo(#"D:\Desktop");
var fullFilePath = dir.GetFiles("*.xlsx").Select(f => f.FullName).First();
if(fullFilePath != null)
Microsoft.Office.Interop.Excel.Workbook workBook = excelApp.Workbooks.Open(fullFilePath);
Hello everyone I'm new to c#. I want to read an xml file if it exists in a directory. 1) How can I read it? 2) If there are multiple xml files how to read those at the same time?
XmlTextReader xtr = new XmlTextReader(path)
string pathD = #"H:\UsersDirectory";
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] TXTFiles = di.GetFiles("*.xml");
if (TXTFiles.Length != 0)
{
//how can I read the file?
}
If you know the name of the file, you can use:
File.Exists("YourPath");
to check if the File exists. If not, you can use:
Directory.GetFiles("ContainingDirectory");
to get a list of all files in a directory, and then loop through them, checking if they end with .xml, to find your file.
As for reading the content of the file, you can use
File.ReadAllText("FilePath");
to read the content of your XML-File. For multiple files, you can obviously just call this function multiple times, once for every file.
If you want to edit XML too, I'd like to direct you towards XPathNavigator: https://learn.microsoft.com/en-us/troubleshoot/developer/visualstudio/csharp/language-compilers/xml-xpathnavigator
My problem is that I have a windows forms application which is located on a shared location, which is doing some logic and at the end i need to export the data into an excel file.
But the excel file should be exported to the machine that the users is logged in, not on the shared server where the application is hosted ...
Any Ideas?
Example of the situation:
The location of the application is at 192.168.1.150\AppName\App.exe
I have access to this shared location and I'm starting the exe file from there.
I need the application to export an excel file to my computer on my desktop .... how?
If you think the folder "My Documents" is a good place to save the Excel file, then this code will help to get you the path:
var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
or if you want to put the file on the Desktop:
var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
The code works regardless of where the folder "My Documents" (or "Desktop") is located for the user (C:, D:, Network share, etc), or which language version his windows installation is using.
To combine with a time based file name:
var fileName = $"your_file_{DateTime.Now:yyMMddHHmmss}.xlsx";
var fullPath = Path.Combine(folderPath, fileName);
I often use time based file names to not overwrite if there is a previous file. (It would of course overwrite if created the same second)
If you want a file name that is guaranteed to be unique you can use a Guid instead of DateTime:
var fileName = $"your_file_{Guid.NewGuid():N}.xlsx";
If the file is just used "within the program" you can also store it in the temporary files folder. To get the path to the temporary files folder you write var folderPath = Path.GetTempPath()
Hope this helps!
Why don't you just use Save File Dialog and save the excel file where you want? Something like this:
private void SaveFile_FileOk(object sender, CancelEventArgs e)
{
string name = SaveFile.FileName;
string[] savearray = new string[] { "some test:" }
File.WriteAllLines(name, savearray);
//this is just an example, your excel file goes here.
}
And on your button to save:
SaveFile.ShowDialog();
You can choose the path for where you want to save...
I'm using C# windows application .
I want to save files in my local system.
I used Open File dialog to attach the files.
Here the text inside the file is copying,I want the file itself to get copied with a new name.But what I am really looking for is , it should just save the file automatically and not show the SaveDialog Box?
How it can be done in windows application.Can anybody help me please?
The code is shown below:
private string GetFileName()
{
OpenFileDialog op1 = new OpenFileDialog();
DialogResult result = op1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
txtEn.Text = op1.FileName;
FileName = op1.FileName;
//MessageBox.Show(FileName);
File.Copy(op1.FileName, #"D:\Backup\");
}
return FileName;
}
SQL Server 2012 seems unrelated to your question. Provided that you have proper access rights to the target directory, then in order to automate the procedure (as per your question) you don't need to use the OpenFileDialog; just a single line should suffice the goal:
//Overwriting a file of the same name is not allowed
File.Copy(FileName, #"D:\Backup\" + FileName)
or
//Overwriting a file of the same name is allowed
File.Copy(FileName, #"D:\Backup\" + FileName, true)
You can also apply some additional logic pertinent to backup file naming (upon necessity).
Hope this may help. Best regards,
Are you trying to copy a file from some x location on your file system to y location (in your case D:\Backup folder) in the file system? If that is the requirement here, I see that you are using the FileName property of OpenFileDialog which gets the File path. This you are appending to D:\Backup. You should instead use the Path.GetFileName property to first extract the file name with extension and then append it to the new folder path
File.Copy(fileName, #"D:\Backup\" + Path.GetFileName(fileName));
I use to store document/file in byte[] in database, and I want user can view/run that file from my application.
You need to know the file extension for the file you're writing, so the OS can run the default program based on the extension. The code would be something like this:
byte[] bytes = GetYourBytesFromDataBase();
string extension = GetYourFileExtension(); //.doc for example
string path = Path.GetTempFileName() + extension;
try
{
using(BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.Create)))
{
writer.Write(yourBytes);
}
// open it with default application based in the
// file extension
Process p = System.Diagnostics.Process.Start(path);
p.Wait();
}
finally
{
//clean the tmp file
File.Delete(path);
}
You will need to store the file extension in the database too. If you don't have the file extension the problem becomes very difficult as you cannot rely on the operating system to work out which program to launch to handle the file.
You can use the following pattern:
Load data from database and save to file using the original file extension.
Start a new System.Diagnostics.Process that points to the saved file path.
As you have saved the file with the original file extension, the OS will look for a program that is registered for the extension to open the file.
As chibacity and Daniel suggest, storing the file extension in the db, and agreed -- storing the file extension, or at least some indicator that tells you the file type, is a good idea.
If these files are of a format of your own creation then you might also want to store information about which version of the file format the data is stored in. During development file formats are prone to changing, and if you don't remember which version you used to store the data then you have a hard job recovering the information.
The same problems are faced in object persistence generally.