C
C#β€’7mo ago
CreamofSumYungGai

❔ How do I import a video file into my Win forms?

I have a WPF project with a mediaElement video imported into it, and I want it so when I press specific buttons on my win forms project, it redirects me to the video WPF project. (they are both separate projects, mind you) If it's not possible to work with a WPF in a winforms, could I use Process.Start to import a file directory into the project? if so, how could I use it?
No description
139 Replies
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
heres what happens when i try to import a video directly from my downloads instead of my WPF form
No description
mtreit
mtreitβ€’7mo ago
Process.Start is for starting executable programs (like files that end with .exe), you seem to be trying to start a xaml file, which doesn't make sense. Also WPF and Winforms are two totally separate tech stacks for building UI programs, you can't really mix them. (Not that I know much about UI programming.)
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Alright is there a way to use Process.Start in directing it to a video file?
mtreit
mtreitβ€’7mo ago
If you use the UseShellExecute option it will try to launch the file similar to what happens when you double-click on a file in Explorer.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Alright, and so after I select the radio button and next it will lead me to the file path I want, correct? (if I followed the steps)
mtreit
mtreitβ€’7mo ago
I have no idea. I don't know what your code is doing.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
No description
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Just the first if Im going to put the code into the first if
mtreit
mtreitβ€’7mo ago
FYI you can prefix strings with an @ symbol to make them 'verbatim' strings, where you don't have to escape the backslashes:
string video = @"c:\users\tiger\downloads\finger.mp4";
string video = @"c:\users\tiger\downloads\finger.mp4";
You probably don't need to embed quotes in the string either.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Okay πŸ‘ I'll test out what your article provided me with I'll update on if it works
Accord
Accordβ€’7mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Does this need to be an .exe? what if it's an mp4 mp4 is the video I'm trying to import
mtreit
mtreitβ€’7mo ago
What do you mean by import? It will launch the mp4 in your default player just as if you had double-clicked on it.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Im trying to write a line of code that opens up the video file directly from my file explorer, as I think the page you showed me is trying to do
mtreit
mtreitβ€’7mo ago
Did you try what I suggested?
mtreit
mtreitβ€’7mo ago
No description
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
OH so I have to set compiler.StartInfo.UseShellExecute = true? because its ran in powershell?
mtreit
mtreitβ€’7mo ago
It has nothing to do with it being run from PowerShell It has to do with the fact that you are trying to 'execute' something that is not a program, and you want Windows to figure out what program to launch based on the file type. ShellExecute is the underlying Win32 API that does this.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
is this code specific to .exe files or if I attach a mp4 file it runs that aswell?
mtreit
mtreitβ€’7mo ago
Is what code specific to exe files?
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
This I want to make sure I can run the mp4 file I want
mtreit
mtreitβ€’7mo ago
🀨 Did you even look at the code in the gif I sent? Here it is for reference:
using System.Diagnostics;

if (args.Length == 0)
{
Console.WriteLine("Provide an mp4 file.");
return;
}

var mp4File = args[0];
var psi = new ProcessStartInfo();
psi.FileName = mp4File;
psi.UseShellExecute = true;
Process.Start(psi);
using System.Diagnostics;

if (args.Length == 0)
{
Console.WriteLine("Provide an mp4 file.");
return;
}

var mp4File = args[0];
var psi = new ProcessStartInfo();
psi.FileName = mp4File;
psi.UseShellExecute = true;
Process.Start(psi);
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Could I ask what the System.Diagnostics is for?
mtreit
mtreitβ€’7mo ago
That's the namespace the ProcessStartInfo and Process types live in.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Ah okay
mtreit
mtreitβ€’7mo ago
(for whatever strange reason)
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
in the windows form there is no args
mtreit
mtreitβ€’7mo ago
This is just me demonstrating how the code works with a simple console program. args is just to get the name of the mp4 file. In your code, do whatever the equivalent would be.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Ah, could I replace the args with the file path? I thought the file path had to be in parenthesis
mtreit
mtreitβ€’7mo ago
Do you know how functions (methods) work? And variables?
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Yes Is there no way of it automatically displaying the file you chose from your file path? instead of having to input it into a console
mtreit
mtreitβ€’7mo ago
The path has to come from somewhere. The important part of the code I showed is this:
var psi = new ProcessStartInfo();
psi.FileName = mp4File; // mp4File is a string variable.
psi.UseShellExecute = true;
Process.Start(psi);
var psi = new ProcessStartInfo();
psi.FileName = mp4File; // mp4File is a string variable.
psi.UseShellExecute = true;
Process.Start(psi);
Get the path into the ProcessStartInfo.FileName property however you like. Like I don't know what you mean here by "displaying the file you chose."
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Displaying the file you chose as in You pick a file you run the engine then it plays the video without you typing in its path Also is process.start not usable anymore to start a file from explorer?
mtreit
mtreitβ€’7mo ago
What do you mean "start a file from explorer" ?
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Im just trying to get my windows forms to run a mp4 I have and since the xaml method doesnt work I want to open it directly from my file explorer I have no idea what to write in terms of the program (visual studios) being able to find the path to the file and opening it
mtreit
mtreitβ€’7mo ago
Visual Studio is the thing you use to write your program. It's not involved when you actually run your program. I'm very confused about what you are asking. Pretend Visual Studio doesn't exist for a moment.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Okay I want to know what I can write in order for it to open up a video file and how do I make it open a video file? does it need to be in a player? If so, what player? I don't want it to say OS isn't compatible or whatever and once it does run the video from MY files, if I were to build this into a .exe program at the end could the user on the other end see the video from MY files?
mtreit
mtreitβ€’7mo ago
I already showed code to open a video file, using the default application registered to play such files. A file is just a series of bytes on disk. Files have paths to them. The program you write needs to get the path from somewhere. Where does it get that path?
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
It's trying to get it from downloads and at mp4File I put in the path to the mp4?
mtreit
mtreitβ€’7mo ago
Yes, the path is just a string.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Uh I put this
No description
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
when I try to run the program it does this
No description
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
No description
mtreit
mtreitβ€’7mo ago
What does the rest of that error message say?
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
there is no error messages for whatever reason
No description
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
should I just restart the program at this rate
mtreit
mtreitβ€’7mo ago
Just copy the entire error message from the output window. It probably says something like the file is in use by another process.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Okay I reopened it
mtreit
mtreitβ€’7mo ago
The process cannot access the file 'bin\Debug\net6.0-windows\Survey FINAL.exe' because it is being used by another process. The file is locked by: "Survey FINAL (12392)" So, you have a copy of that program running Which means you can't overwrite it.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
I do??? how do I get rid of the other program
mtreit
mtreitβ€’7mo ago
Get-Process | ?{$_.Id -eq 12392} | Stop-Process
Get-Process | ?{$_.Id -eq 12392} | Stop-Process
(probably)
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
apply that at the very outside of the code??
mtreit
mtreitβ€’7mo ago
Run that from a PowerShell window Or go use Task Manager to find process id 12392 and terminate it. If you aren't a CLI type.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
OH OKAY THANK YOU PID is what this is? OMG IT WORKED YESSSS Can I try to understand what this did So I didn't need to put in a path to VLC or media player or whatever, it just automatically does that for me? And also now that the video works I want to build this into a .exe and send it to my friend can they also access the video after they click the button? even though the directory is in my files
mtreit
mtreitβ€’7mo ago
If you really want to understand how it works, here is the documentation on what it is doing behind the scenes: https://learn.microsoft.com/en-us/windows/win32/shell/launch
Launching Applications (ShellExecute, ShellExecuteEx, SHELLEXECUTEI...
Once your application has located a file object, the next step is often to act on it in some way.
mtreit
mtreitβ€’7mo ago
And no, the program doesn't magically contain the video file, it just contains the path to the video file. Which may or may not be valid on other computers.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
So how do I make it so its accessable on all computers windows related? (not mac OS ofc, I have no idea how its executed on there)
mtreit
mtreitβ€’7mo ago
You want your program to contain the actual video file?
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Yes Its lovely I can play a video now but I want it to play thruout computers
mtreit
mtreitβ€’7mo ago
How are you distributing the program? Just include the mp4 file with the binaries for your program.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
distributing? Oh I am sending it via winrar through the bin
mtreit
mtreitβ€’7mo ago
So, you would want to do something like copy the mp4 into the bin folder before you winrar it, and change your program's code to look for it in the folder from which it is executing. Something like this:
var mp4File = Path.Combine(AppContext.BaseDirectory, "video.mp4");
var mp4File = Path.Combine(AppContext.BaseDirectory, "video.mp4");
Replace "video.mp4" with whatever your video name is. Now when you run the program it will look for the mp4 in the same folder as the program.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Ohhh okay I see so the video comes along with the package
mtreit
mtreitβ€’7mo ago
Yeah I mean, if it's not huge you could base64 encode it into the source code itself or something but usually video files are not small.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
just to make sure whenever you write video.mp4 it's always the direct path to the video right but in this case would I still use the same path? which is to my downloads
mtreit
mtreitβ€’7mo ago
No, this is saying "figure out what folder this code is running from and look for the file 'video.mp4' in that same folder."
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Which is when I put it into the bin right
mtreit
mtreitβ€’7mo ago
So as long as you zip up the program binaries and the mp4 file is in the same folder (bin\release or whatever) then when someone unzips it and runs the program it will be able to find the file (since the executable and the mp4 are co-located in the same folder.)
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
When I build it into a solution though wouldn't it create a brand new project in the bin? Oh wait do I keep it as the mp4's name So then I just put (in my case)
var mp4File = Path.Combine(AppContext.BaseDirectory, "finger.mp4");
var mp4File = Path.Combine(AppContext.BaseDirectory, "finger.mp4");
mtreit
mtreitβ€’7mo ago
Sure
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
this makes a lot more sense I thought I had to create a solution THEN change up the code in the solution to whatever path the video coexisting in the folder had Could you run my program as it is right now to see if it works? as in playing the video file I put
mtreit
mtreitβ€’7mo ago
As a general rule, no. I would ask one of your friends to help you test it.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Ah okay And if I make a new release in the bin (Like if I add more stuff to the project) I can just copy and paste the video in the folder and then zip it again? and it will still look for the video name finger.mp4 the same
mtreit
mtreitβ€’7mo ago
Yes. You can also set up your project to copy the file into the bin folder as part of the build.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
how is that done? sounds more convenient
mtreit
mtreitβ€’7mo ago
<ItemGroup>
<None Update="video.mp4">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Update="video.mp4">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
You put something like that in your csproj file
mtreit
mtreitβ€’7mo ago
Visual Studio has a UI for that:
No description
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
this is in windows forms too?
mtreit
mtreitβ€’7mo ago
It's for any C# project
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Okk thank youu Ok so I made my friend test it
mtreit
mtreitβ€’7mo ago
Did it work?
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Yes overall but the video did not
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
No description
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
finger mp4 was in the folder before I winrar'd it
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
I wonder what happened is it because saying "finger.mp4" is too vague?
mtreit
mtreitβ€’7mo ago
Why is it looking in the downloads folder? Did you zip the wrong binaries that didn't have the code change to get the path from AppContext?
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
let me see
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
No description
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
No description
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
they probably extracted it wrong or dont have winrar right? You need winrar to properly extract .exe files They downloaded winrar and same error occurs fyi I copied the mp4 file path originally from download into the project and in the project folder And I followed your advice on simply putting β€œfinger.mp4” so that it would find the file called that Not sure why it didnt find finger.mp4 though in said winrar?
mtreit
mtreitβ€’7mo ago
The error message says it's looking in the downloads folder. That implies that path is still in the code. Which is wrong. Which means either the code still has that path hardcoded into it or you put the wrong files in the winrar container.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Should I remove these lines of code then? Since here it asks for my computers path
Accord
Accordβ€’7mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Hello?
mtreit
mtreitβ€’7mo ago
Share the full code and someone can probably explain why you are getting that error.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
for the specific form or for each and every single form
mtreit
mtreitβ€’7mo ago
If it's possible to share the entire project and/or the rar file I can probably point out what you're doing wrong.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Okay πŸ‘Œ How do I send you the large file I don't have nitro I can send it thru email
mtreit
mtreitβ€’7mo ago
How big is the file?
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Let's see
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
No description
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
11,887 KB right now
mtreit
mtreitβ€’7mo ago
So 12MB
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
It went up
No description
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
120,703 KB what
mtreit
mtreitβ€’7mo ago
What changed?
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Wait I think i accidentally placed a file inside of a file I got nitro free trial I'll send it here
mtreit
mtreitβ€’7mo ago
if (this.radio1.Checked)
{
string str = "C:\\Users\\tiger\\Downloads\\finger.mp4";
Path.Combine(AppContext.BaseDirectory, "finger.mp4");
Process.Start(new ProcessStartInfo()
{
UseShellExecute = true,
FileName = str
});
this.Hide();
this.Close();
}
if (this.radio1.Checked)
{
string str = "C:\\Users\\tiger\\Downloads\\finger.mp4";
Path.Combine(AppContext.BaseDirectory, "finger.mp4");
Process.Start(new ProcessStartInfo()
{
UseShellExecute = true,
FileName = str
});
this.Hide();
this.Close();
}
😐 Look at that code closely and think about what it does.
string str = "C:\\Users\\tiger\\Downloads\\finger.mp4";
Path.Combine(AppContext.BaseDirectory, "finger.mp4");
Process.Start(new ProcessStartInfo()
{
UseShellExecute = true,
FileName = str
});
string str = "C:\\Users\\tiger\\Downloads\\finger.mp4";
Path.Combine(AppContext.BaseDirectory, "finger.mp4");
Process.Start(new ProcessStartInfo()
{
UseShellExecute = true,
FileName = str
});
Specifically this.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
specify this? here I mean
mtreit
mtreitβ€’7mo ago
This string should not exist in your source code:
"C:\\Users\\tiger\\Downloads\\finger.mp4"
"C:\\Users\\tiger\\Downloads\\finger.mp4"
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
What the why's there parenthesis after brackets
mtreit
mtreitβ€’7mo ago
You're still trying to load the file from the downloads folder. Which is exactly what the error message told you.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Yes
mtreit
mtreitβ€’7mo ago
Well you didn't give me the source code so I had to decompile it and that's how the decompiler wrote the C# code. So it might look slightly different than what your actual code looks like.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Ohh okay
mtreit
mtreitβ€’7mo ago
This:
string str = "C:\\Users\\tiger\\Downloads\\finger.mp4";
Path.Combine(AppContext.BaseDirectory, "finger.mp4");
string str = "C:\\Users\\tiger\\Downloads\\finger.mp4";
Path.Combine(AppContext.BaseDirectory, "finger.mp4");
Should be replaced with this:
var str = Path.Combine(AppContext.BaseDirectory, "finger.mp4");
var str = Path.Combine(AppContext.BaseDirectory, "finger.mp4");
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
So if I deleted this string, then should I change the psi to the downloadsFolder path? or do I must create an entirely new one Ah So then the psi.FileName = str
mtreit
mtreitβ€’7mo ago
You already had that part, but yes.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
huuh..?
No description
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
It can't find finger.mp4 if I don't give it the exact path from my folders :/ (on my end)
mtreit
mtreitβ€’7mo ago
Well, is finger.mp4 present in that folder?
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
If I put finger.mp4 in the same project then wouldn't I need to change another string's path to the directory? string downloadsFolder = (path blah blah)
mtreit
mtreitβ€’7mo ago
dir "C:\Users\tiger\source\repos\Survey FINAL\Survey FINAL\bin\Debug\net6.0-windows\finger.mp4"
dir "C:\Users\tiger\source\repos\Survey FINAL\Survey FINAL\bin\Debug\net6.0-windows\finger.mp4"
Like does that show an error in PowerShell?
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
YESS put it in Survey FINAL or the bin of Survey FINAL?
mtreit
mtreitβ€’7mo ago
I mean, this is the path where the file needs to exist: "C:\Users\tiger\source\repos\Survey FINAL\Survey FINAL\bin\Debug\net6.0-windows\finger.mp4" Assuming I typed it by hand correctly looking at your screenshot. So copy the file there And make sure this succeeds:
dir "C:\Users\tiger\source\repos\Survey FINAL\Survey FINAL\bin\Debug\net6.0-windows\finger.mp4"
dir "C:\Users\tiger\source\repos\Survey FINAL\Survey FINAL\bin\Debug\net6.0-windows\finger.mp4"
Then run your program.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
I see so in the debug folder IT RAN
mtreit
mtreitβ€’7mo ago
The rar file you sent me has the mp4 file in the net6.0-windows folder, which looked correct. You probably want to change your csproj to always copy that file to the output folder on build. I showed how to do that yesterday.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
So now since the video is in the original bin of the project, as I zip the .exe for others to run it will also show up on their end because the path of the video isn't specific to my end Okay This doesn't show up automatically on my end although I've pasted the video into my project's bin do I have to manually import it into my project?
mtreit
mtreitβ€’7mo ago
You use "Add existing item" in Visual Studio On the right-click context menu for the project
mtreit
mtreitβ€’7mo ago
When you add it, you can choose to add a link instead of to copy the file into the same folder as your source code:
No description
mtreit
mtreitβ€’7mo ago
If you don't want large binary files checked in to your git repo or whatever (which is usually a bad practice.)
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
What does copy if newer mean? Whenever someone downloads the application isn't the file technically always going to be newer?
mtreit
mtreitβ€’7mo ago
That only applies when you compile the program. It won't copy the file into the output directory if the timestamp on the existing file is the same as the one that you added. It speeds up build times by removing excessive file copies. It has nothing to do with someone downloading your program.
CreamofSumYungGai
CreamofSumYungGaiβ€’7mo ago
Ah okay is there a way to automatically end the video after it's done playing? ?
Accord
Accordβ€’7mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.