简体   繁体   中英

Open a file in C# using Process.Start

I am writing a program that monitors a folder and lets you know when a file is created. I'm struggling to open the file when the user clicks ok. Please could I have advice on how to get the Process.Start() to work, i'm trying to get the file location to load a text file from e.Fullpath and open in Notepad.

private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
{
    DialogResult messageresult = MessageBox.Show("You have a Collection Form: " + e.Name);
    if (messageresult == DialogResult.OK)
        Process.Start("Notepad.exe", "e.FullPath");
}

尝试Process.Start("Notepad.exe", e.FullPath);

The second parameter of Process.Start is a string, but you are passing a string type, so you do not need to use the " marks around it.

Only string literals such as your first argument require quotation marks around them.

string notepadPath = Path.Combine(Environment.SystemDirectory, "notepad.exe");
if (File.Exists(notepadPath))
    Process.Start(notepadPath, e.FullPath);
else
    throw new Exception("Can't locate Notepad");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM