β 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?
139 Replies
heres what happens when i try to import a video directly from my downloads instead of my WPF form
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.)
Alright
is there a way to use Process.Start in directing it to a video file?
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.
ProcessStartInfo.UseShellExecute Property (System.Diagnostics)
Gets or sets a value indicating whether to use the operating system shell to start the process.
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)
I have no idea. I don't know what your code is doing.
Just the first if
Im going to put the code into the first if
FYI you can prefix strings with an
@
symbol to make them 'verbatim' strings, where you don't have to escape the backslashes:
You probably don't need to embed quotes in the string either.Okay π
I'll test out what your article provided me with
I'll update on if it works
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.Does this need to be an .exe? what if it's an mp4
mp4 is the video I'm trying to import
What do you mean by import?
It will launch the mp4 in your default player just as if you had double-clicked on it.
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
Did you try what I suggested?
OH so I have to set compiler.StartInfo.UseShellExecute = true?
because its ran in powershell?
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.
is this code specific to .exe files or if I attach a mp4 file it runs that aswell?
Is what code specific to exe files?
This
I want to make sure I can run the mp4 file I want
π€¨
Did you even look at the code in the gif I sent?
Here it is for reference:
Could I ask what the System.Diagnostics is for?
That's the namespace the ProcessStartInfo and Process types live in.
Ah okay
(for whatever strange reason)
in the windows form there is no args
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.
Ah, could I replace the args with the file path?
I thought the file path had to be in parenthesis
Do you know how functions (methods) work?
And variables?
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
The path has to come from somewhere. The important part of the code I showed is this:
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."
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?
What do you mean "start a file from explorer" ?
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
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.
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?
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?
It's trying to get it from downloads
and at mp4File I put in the path to the mp4?
Yes, the path is just a string.
Uh I put this
when I try to run the program it does this
What does the rest of that error message say?
there is no error messages for whatever reason
should I just restart the program at this rate
Just copy the entire error message from the output window. It probably says something like the file is in use by another process.
Okay I reopened it
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.
I do???
how do I get rid of the other program
(probably)
apply that at the very outside of the code??
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.
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
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.
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.
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)
You want your program to contain the actual video file?
Yes
Its lovely I can play a video now but I want it to play thruout computers
How are you distributing the program? Just include the mp4 file with the binaries for your program.
distributing?
Oh I am sending it via winrar
through the bin
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:
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.
Ohhh
okay I see
so the video comes along with the package
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.
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
No, this is saying "figure out what folder this code is running from and look for the file 'video.mp4' in that same folder."
Which is when I put it into the bin right
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.)
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)
Sure
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
As a general rule, no. I would ask one of your friends to help you test it.
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
Yes. You can also set up your project to copy the file into the bin folder as part of the build.
how is that done?
sounds more convenient
You put something like that in your csproj file
Visual Studio has a UI for that:
this is in windows forms too?
It's for any C# project
Okk thank youu
Ok so I made my friend test it
Did it work?
Yes overall but the video did not
finger mp4 was in the folder before I winrar'd it
I wonder what happened
is it because saying "finger.mp4" is too vague?
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?
let me see
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?
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.
Should I remove these lines of code then?
Since here it asks for my computers path
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.Hello?
Share the full code and someone can probably explain why you are getting that error.
for the specific form or for each and every single form
If it's possible to share the entire project and/or the rar file I can probably point out what you're doing wrong.
Okay π
How do I send you the large file
I don't have nitro
I can send it thru email
How big is the file?
Let's see
11,887 KB right now
So 12MB
It went up
120,703 KB what
What changed?
Wait
I think i accidentally placed a file inside of a file
I got nitro free trial I'll send it here
π
Look at that code closely and think about what it does.
Specifically this.
specify this?
here I mean
This string should not exist in your source code:
What the
why's there parenthesis after brackets
You're still trying to load the file from the downloads folder.
Which is exactly what the error message told you.
Yes
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.
Ohh okay
This:
Should be replaced with this:
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
You already had that part, but yes.
huuh..?
It can't find finger.mp4 if I don't give it the exact path from my folders :/ (on my end)
Well, is finger.mp4 present in that folder?
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)
Like does that show an error in PowerShell?
YESS
put it in Survey FINAL or the bin of Survey FINAL?
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:
Then run your program.
I see
so in the debug folder
IT RAN
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.
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?
You use "Add existing item" in Visual Studio
On the right-click context menu for the project
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:
If you don't want large binary files checked in to your git repo or whatever (which is usually a bad practice.)
What does copy if newer mean? Whenever someone downloads the application isn't the file technically always going to be newer?
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.
Ah okay
is there a way to automatically end the video after it's done playing?
?
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.