โ Code Not Writing To Text File
So I'm very new to c# (Just Started A couple hours ago) I code other languages though so I'm not a complete beginner but basically all I need help with is diagnosing and fixing why my method wont write to a text file
169 Replies
Since you're just starting with C# and simply writing text to a file, I recommend using the simpler
File.WriteAllText
method.File.WriteAllText Method (System.IO)
Creates a new file, write the contents to the file, and then closes the file. If the target file already exists, it is overwritten.
Its multiple inputs over an infinite span of time
Look up StreamWriter or any of the File.WriteAllโฆ. Methods
its a data tracker so as data is inputted it needs to append it to a new line
whats the true problem you trying to achieve then?
I did it in python
ill send that code so maybe u can kinda understand'
whenever you get nrw input just take out all text string append and put back imo
File.AppendText(String) Method (System.IO)
Creates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist.
God no
yea no
that seems inefficent as all hell
See my link above
Why can't you do something like that?
Solve performance problems when they arise, not ahead of time
think about how many lines could come and how inefficent that would end up being
He doesnโt need to do that though
Is this for practice or a production level application?
idk if you truly need a txt file for it
a db sounds more appropriate here
My understanding was that this was for learning
practice but I like to get dropped into the deep end
you have table data
This TBH
how do you make a db
ef core or dapper most commonly ef core
Itโs more steps
Well that's how to interact with it
But do you want to make a text file just as a basic log?
Depends on which provider you want to go with
no no db is good I would like to learn that too
any resources for that?
SQL server is pretty common in the .NET world
just search "ef core"
Then use Microsoft.Data.SqlClient
and you get a whole load of resources
sqlite for anything smaller
MongoDB
Learners should not skip the foundations as to why we got to EF Core
eww
Vomit
i never wrote a single line of sql myself
You should
๐
Congrats. You would be expected to as a full-stack where I am.
Not saying every day
But good knowledge to have at least
iam a full stack as well
Especially for someone learning
Not if you've never written any SQL
Congrats. I write SQL when I have to, which is rare but I know how to
i do as well know how to as i can read it but i never needed to write it
That is rare and really not common
I have to use Dapper because my boss won't let us use EF ๐ญ
Unless other people are doing it for you
You have my pity
So I still have deal with it by hand okay
That's been most of my career
clone ef by hand
I get to maintain EF Core and sprocs
For SSRS
My early career exposed me to the likes of
SqlBulkCopy
๐Canโt use ORMs for SSRS
maybe we should go back to #chat
Don't disagree
@Solar, did we answer your questions?
I know we derailed a bit
"a bit" yes
Im currently trying to figure out efcore
if any questions further just ping us
Best of luck
Honestly its just an application for sims lol
my girlfriend plays so I wanted to make her a thing that keeps track of paintings and her net gain etc
Yeah, my teaching style is โHeres the best method to do this, but hereโs why we do it this wayโ
you forgot the "p" there
huh
wait do i need to buy something in order for efcore to work properly
like do i need to have a server
You know MS still recommends reviewing migration code. The classes and such
With EF Core
i am reading it now
You need SQL Server Developer edition which is free
I would say get 2022
ef core is just a mapper between your classes and a SQL server or whatever data source you have
you maybe want an sql server (simply spin up using docker) or use a sql lite single file based db
Or MySQL Community 8
Gross
Both are free
Idk which one to get lmao i got this one
is that right'
Thatโs right. I would go with Basic since youโve never setup SQL server before
woah woah woah bold of you to assume
Or if you want to follow a tutorial and do custom
I haven't but still bold of you to assume lmao
basic = install here with defaults
custom = set some settings prior to install
download = download the installer to install it on a server that doesnt have internet access
I donโt have to since you asked โDo I have to have a server?โ When we said to get a
lol
wait can i upload movies to an sql server
As a blob? Sure
yes
so basically I could make walmart brand plex
sql server is more of a dump in me anything storage
my autocorrect on phone almost made this nsfw whoops
Anything except for all my regrets
I mean
Wat
i read the download wrong and it looked like it said 1.1 mil mb download
Canโt remember if thereโs a screen for basic install or not that asks you what kind of authentication you want to use
Iโve always done Custom
But I would recommend going with Mixed Mode Authentication
so Ill do the server later but for now what would be the best way for text file
The link I posted last
its just i have mcdonalds wifi
File.AppendText
If all you need to do is add to the next line
And not view the contents after
so looking at ur link ive done what it says
and it still wont work
i still recommend running it as a docker container more cause a dev db you dont need to constantly eat up resources
That can come later
Let him figure out this first
Docker has nothing to do with the requirements of what heโs trying to learn
just for the dev db there as a suggestion
plus as he isnt a total dev newbie he might already know docker
oh its the path
I think at least the directory path has to exist. If the file doesnโt it will make it
depends on the dotnet version as far as i remember
Yeh he needs to read that documentation
Thatโs for latest
yep
so how do i make a path that means its in the same folder as the file of text
Look up Path.Combine()
var path = Path.Combine(your, path, parts)
if (Path.Exists(path)
{
blah
}
File, Path, Directory are all classes you would likely use here
filestream as well maybe
Definitely File and Path
no no i mean if i put it on another pc i want it to work but the path wont be the same so how do i make it figure out the path that its in
Ah that probables
Problem
Relative paths
yessir
I used to know how to do em but i forgot em lol
you always think of the base as where your .exe is located (or whatever you compile to)
and from there you go up 1 folder with ./../
I usually do
var path = Path.Combine(AppDomain.Current.BaseDirectory, "path", "to", "my", "file.txt");
and down with well its name
but what if they put it on the desktop
and i put it in my photos
Where do you want it to go?
uh so id want the exe to go on desktop and the file to go in program files i guess
FileSystem.SpecialDirectories.Desktop exists i think
unless that was a maui specific thing
Yeah see if C#/.NET has a way to get you an absolute path to Desktop
the thing is it is user based so the path will have their windows user name in it
idc abt user based
just computer based
for now
Im telling you
Windows Desktop path is user specific
as in current user
oh
nope there is both
there is user based and a generic for all
can I just make it a relative path to the folder and it loads from where ever the folder is put
or does it have to be full path
The public profile?
yes
well yeah, thats still a user in the users directoryt
sorta
if i did something like /ProjectFolder/Textfile.txt/ would that be enough
everything inside the public profile is always on every user as well
you need to be an admin to remove it from there tho
One thing I hate about Windows 11
They dont want you setting up a local user
You can put the file anywhere
just setup using for company
put it in System32
i dare you
then it lets you
na
i put my data always on C:\<companyname>\<appname>\data
I have 4 letterd drives, 2 of which are RAID1?
RAID0?
i dont remember
8 TB for games under 1 letter
mmmmmmmmmmmmmmmmmmmmmmmmmmm
so i did what i said and it says could not find part of path but then literally shows the entire path
enter debug mode, put a breakpoint where you are making the path, read it, open up explorer and manually see if you can go ther
if not
theres your problem
it says excepetion unhandled
if you can, see @Hazel | ใธใใใ
I just manually typed the path
string path = "HelloWorld/HelloWorld/paintingHistory.txt";
thats not going to work
it should ๐ฆ
File.AppendText needs an absolute path
what you gave it is not
im assuming this is a console app type project?
yes
and you want the file to be saved in the same directory that it is in?
yes
then this needs to be your path
ok but can you explain why too
and why it matters that its a console app
string path = Path.Combine(AppDomain.Current.BaseDirectory, "paintingHistory.txt");
I was just wondering, has nothing to do with what kind of app it ismaybe its CurrentDomain
use autocomplete to tell you
yea it suggested that wanted to make sure tho
autocomplete is pretty much right
unless someone messed up
that didnt work
nothing was entered
you're supposed to be using
File.AppendText
not File.Create
nono thats my checking if it exists the append is lower
come to think of it its not doing the file exists output either
whats the c# equiv of sleep
Thread.Sleep
thx
Most external operations should be
async
though, which gives you access to Task.Delay
instead
Since you like diving off the deep end, I'd look into that at some point toook so it says file exists
so now the only mystery is why its not writing
It's not going to be in the location where the Program.cs is
if you ran a Debug build it will be in the bin/Debug/netwhatever location
i figured it out
yea i think so
how do i run in non debug
I'll let you google that
im going to take a shower now
ok i did it
but
it still says
file exists
when i deleted it
I have idea
I have made it print out the path
alr it works now thx guys!
welcome to the party, BTW
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.