Access the files directory that showed in Recent Files - c#

I have made a c# app to access files that was in the Recent Files. But i need to open the file when clicked. How can i do it?
Code : https://imgur.com/a/rSfSRrx
NB: My app shows files that has been used by the user recently. The name of the file he used will be showed when user switches on the pc next time. So that he can access it quickly and easily. I have accessed the Recently Used Files in windows and retrieved the file name. But I don't know about the code to open that file. That's what I am asking. File name is directly retrieved from the recent files.

Ideally you should be using the IShellFolder interface to enumerate and execute items in the Recent folder but I assume you are not going to do that and instead keep the code you already have.
Environment.SpecialFolder.Recent is indeed the folder where these files are kept. These .lnk files are shortcuts and when you want to execute them you must make sure you have a full path (Environment.SpecialFolder.Recent + filename + ".lnk") and pass this to Process.Start:
using System.Diagnostics;
...
Process myProcess = new Process()
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.FileName = fullpathtoshortcut;
myProcess.Start();
If you want to be a good citizen and do this properly with the shell interfaces you probably need to get the shell API Code Pack.

Related

How to get directory of an xls file C#

I need to make a program that reads the columns of an excel. But for that, I need to get the path (directory) of this excel. What prevents me from doing this, is that I didn't want to leave my local directory fixed, because if someone downloads the file on another machine, they will need to change the path.
You asked How to get directory of an xls file C#.
As others have pointed out, the file needs to be in a known location and one way to do that is to add the Excel file to your solution and mark it as Content, specifying that it should be copied to the output directory. The image below shows how to set these properties.
You also seem to be looking for a robust way to make this happen when you deploy the app for other users. For this, you will need a strategy for making an .msi or .msix that will place this file where it needs to be. But to answer your basic question the way you asked it the path in this case can be obtained in this manner. This code will get the path and open the Excel file.
// Get the path and open the Excel file
static void Main(string[] args)
{
var path =
Path.Combine(
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
"Microsoft Excel Worksheet.xlsx");
Console.WriteLine(path);
System.Diagnostics.Process.Start("explorer.exe", path);
}
I should mention that a file in the executing directory can be read but not written (without elevated permissions). If the file is user data then refer to the answer that uses an Environment variable:
var appdata = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
You have two options:
If you don't want to your excel files to be stored in your application's directory, you can simply put things in the root of your C: partition. Every windows device is gonna have the C: partition. As the user Lee Taylor has pointed out, it turns out you can have a windows device without the C: partition, although uncommon
If you don't mind having your excel files stored in your application's directory, you can get the relative path of your application's directory throughPath.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
There are other ways but I believe these are the simplest and easiest to do.
If you just need to dynamically get the path to a file that is in the same project directory, then the following code works for me -
string filePath = Directory.GetParent(Environment.CurrentDirectory).Parent.FullName + "\\excel\\Prices.xlsx";
where "\excel" is the folder in my project and "\Prices.xlsx" the file in the project

How to delete files in the Downloads folder

I've managed to use the WinAPI SHGetKnownFolderPath() method to get the path to the Downloads folder, but when using the following code, I can't get it to delete specific types of files:
string rootFolderPath = KnownFolders.GetPath(KnownFolder.Downloads);
**string filesToDelete = #"*Agreement, CCRPCI, SECCI, Debit*.pdf";**
string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, filesToDelete);
foreach(string file in fileList)
{
System.IO.File.Delete(file);
}
What do I need to add / change in order to reference multiple different file names? I know for a fact the bit I've highlighted in bold is incorrect.
EDITED -
Is there a way to delete files with different file names but the same extension type? As for the reason behind why I need to do this - My automation tests download various different files which get saved into the downloads folder. As the volume of automation tests is high these documents start to take up a lot of storage. Currently I'm going in and manually deleting them. I'm very new to coding guys and to this forum, I know it's a lot to ask but please have patience with me as I'm only trying to learn. (Already had two down votes)
Is there a way to delete files with different file names but the same
extension type?
You can use the shell commands, del DownloadsPath\\*.xxx and run it in your program. And here is the method to run shell commands in C#.
Or run a bat file directly.

Make exe file of app from another app

I want to develop an application, App1, that gets some information & settings from the user. Then, using these settings, it should build a second application, App2. I want to have App1 build App2's exe file.
I know that one way to do this is to make a text or XML file to hold the settings and put this next to App2's exe file, but I want to embed these settings into App2's exe file instead. How can I do this using Visual Studio, the .net framework, and the C# language?
I don't understand what do you mean by those user information and settings (some example would help), but basically, what you want to do is invoke the C# compiler as follows
var p = new Process();
p.StartInfo.FileName = #"c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe";
p.StartInfo.Arguments = #"c:\aa\Test.cs";
p.Start();
These four lines invoke the c# compiler on a C# code file and produces an exe file in the output folder for your project.
MSDN provides more info on how to work with the compiler via command line. Especially, how to compile more than one file, how to compile a library, etc.
You can accomplish the same thing like this:
Make two applications. App2 will be the same .exe file for all cases of App1. Instead of building an .exe file, App1 will generate a configuration file containing the information and settings input by the user. This file will be read by App2. App2 will then call the appropriate functions within App2, based on the information and settings in the configuration file.
I'm trying to do the same thing for the same reasons, but using VB.NET. That's how I found this question. The only way I can thing of (other than editing the source code each time, which is what I'll be doing unless I find a better way), is to create a second program that encrypts a parameter file and give the encrypted parameter file and the exe file to the end user.
I'm still using the "editing the source method" because I want to give the user just the exe file without dealing with parameter files or encryption. There aren't many users who will need this in my case (for the moment) so I can deal with that method for now.

How can Windows file and folder shortcuts be created via C#.NET code?

I'm building a repository folder and file structure with many dependencies in version control using .NET for our data warehouse. Currently I have code to create dummy files and folders with C# code (see below). However, there are objects being shared. So I'd like to create shortcuts to Windows files and shortcuts to Windows folders as well as files. What would the code look like in C# to accomplish this?
Create Folder via C#.NET code:
string activeDir = #"C:\Source\EDW\dw-objects\trunk\table-objects";
string objectName = reader[0].ToString();
string newPath = System.IO.Path.Combine(activeDir, objectName);
System.IO.Directory.CreateDirectory(newPath);
Code varies depending on files based on format
Please use the ShellClass to create shortcuts also you will
need to get the special directory from desktop using Environment.SpecialFolder.DesktopDirectory
A very good example showing step by step can be found here http://www.codeproject.com/Articles/146757/Add-Remove-Startup-Folder-Shortcut-to-Your-App
Have a look at This Article it shows you how to manipulate NTFS Juncture points using c#. Because you will want to access the folders you need to have juncture points instead of shrotcuts since one is followed by machine processing and one isn't.

How to have a program know where it has been launched?

Imagine I have a picture viewer application made with C# and .NET. I have already set the preferred application to view pictures to use the C# application.
I want to somehow let my program know where it has been invoked. How can I achieve this?
If you're using it to view pictures via shell associations, you can just check the picture filenames passed in on the command line. You can use Environment.GetCommandLineArgs to get the first filename:
// Should check to make sure there is at least one filename passed first...
string imageFilename = Environment.GetCommandLineArgs[1];
string directory = System.IO.Path.GetDirectoryName(imageFilename);
If you want the working directory, just check Environment.CurrentDirectory at startup...
I think you can use Environment.CurrentDirectory
The current directory (Environment.CurrentDirectory) of an application can change during execution. Additionally, the current directory may not be the directory in which the application resides, such as if a user runs it from a command line in an arbitrary directory by specifying an absolute path to the executable.
If you really want the "current directory" of the application, then use Environment.CurrentDirectory, but if you want to know the location of the application, you can use the following approaches:
System.Windows.Forms.Application.ExecutablePath
(if running a WinForm application)
System.Windows.Forms.Application.StartupPath
(if running a WinForm application)
System.Reflection.Assembly.GetEntryAssembly().Location

Categories

Resources